home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2002 November / CD 1 / APC0211D1.ISO / workshop / prog / files / ActivePerl-5.6.1.633-MSWin32.msi / _b52d16df54da0e960fc2264e29c0157e < prev    next >
Encoding:
Text File  |  2001-09-04  |  219.6 KB  |  6,993 lines

  1. # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
  2. package CPAN;
  3. $VERSION = '1.59_54';
  4. # $Id: CPAN.pm,v 1.385 2001/02/09 21:37:57 k Exp $
  5.  
  6. # only used during development:
  7. $Revision = "";
  8. # $Revision = "[".substr(q$Revision: 1.385 $, 10)."]";
  9.  
  10. use Carp ();
  11. use Config ();
  12. use Cwd ();
  13. use DirHandle;
  14. use Exporter ();
  15. use ExtUtils::MakeMaker (); # $SelfLoader::DEBUG=1;
  16. use File::Basename ();
  17. use File::Copy ();
  18. use File::Find;
  19. use File::Path ();
  20. use FileHandle ();
  21. use Safe ();
  22. use Text::ParseWords ();
  23. use Text::Wrap;
  24. use File::Spec;
  25. no lib "."; # we need to run chdir all over and we would get at wrong
  26.             # libraries there
  27.  
  28. END { $End++; &cleanup; }
  29.  
  30. %CPAN::DEBUG = qw[
  31.           CPAN              1
  32.           Index             2
  33.           InfoObj           4
  34.           Author            8
  35.           Distribution     16
  36.           Bundle           32
  37.           Module           64
  38.           CacheMgr        128
  39.           Complete        256
  40.           FTP             512
  41.           Shell          1024
  42.           Eval           2048
  43.           Config         4096
  44.           Tarzip         8192
  45.           Version       16384
  46.           Queue         32768
  47. ];
  48.  
  49. $CPAN::DEBUG ||= 0;
  50. $CPAN::Signal ||= 0;
  51. $CPAN::Frontend ||= "CPAN::Shell";
  52. $CPAN::Defaultsite ||= "ftp://ftp.perl.org/pub/CPAN";
  53.  
  54. package CPAN;
  55. use strict qw(vars);
  56.  
  57. use vars qw($VERSION @EXPORT $AUTOLOAD $DEBUG $META $HAS_USABLE $term
  58.             $Revision $Signal $End $Suppress_readline $Frontend
  59.             $Defaultsite $Have_warned);
  60.  
  61. @CPAN::ISA = qw(CPAN::Debug Exporter);
  62.  
  63. @EXPORT = qw(
  64.          autobundle bundle expand force get cvs_import
  65.          install make readme recompile shell test clean
  66.         );
  67.  
  68. #-> sub CPAN::AUTOLOAD ;
  69. sub AUTOLOAD {
  70.     my($l) = $AUTOLOAD;
  71.     $l =~ s/.*:://;
  72.     my(%EXPORT);
  73.     @EXPORT{@EXPORT} = '';
  74.     CPAN::Config->load unless $CPAN::Config_loaded++;
  75.     if (exists $EXPORT{$l}){
  76.     CPAN::Shell->$l(@_);
  77.     } else {
  78.     $CPAN::Frontend->mywarn(qq{Unknown command "$AUTOLOAD". }.
  79.                 qq{Type ? for help.
  80. });
  81.     }
  82. }
  83.  
  84. #-> sub CPAN::shell ;
  85. sub shell {
  86.     my($self) = @_;
  87.     $Suppress_readline = ! -t STDIN unless defined $Suppress_readline;
  88.     CPAN::Config->load unless $CPAN::Config_loaded++;
  89.  
  90.     my $oprompt = shift || "cpan> ";
  91.     my $prompt = $oprompt;
  92.     my $commandline = shift || "";
  93.  
  94.     local($^W) = 1;
  95.     unless ($Suppress_readline) {
  96.     require Term::ReadLine;
  97.         if (! $term
  98.             or
  99.             $term->ReadLine eq "Term::ReadLine::Stub"
  100.            ) {
  101.             $term = Term::ReadLine->new('CPAN Monitor');
  102.         }
  103.     if ($term->ReadLine eq "Term::ReadLine::Gnu") {
  104.         my $attribs = $term->Attribs;
  105.          $attribs->{attempted_completion_function} = sub {
  106.          &CPAN::Complete::gnu_cpl;
  107.          }
  108.     } else {
  109.         $readline::rl_completion_function =
  110.         $readline::rl_completion_function = 'CPAN::Complete::cpl';
  111.     }
  112.     # $term->OUT is autoflushed anyway
  113.     my $odef = select STDERR;
  114.     $| = 1;
  115.     select STDOUT;
  116.     $| = 1;
  117.     select $odef;
  118.     }
  119.  
  120.     # no strict; # I do not recall why no strict was here (2000-09-03)
  121.     $META->checklock();
  122.     my $cwd = CPAN::anycwd();
  123.     my $try_detect_readline;
  124.     $try_detect_readline = $term->ReadLine eq "Term::ReadLine::Stub" if $term;
  125.     my $rl_avail = $Suppress_readline ? "suppressed" :
  126.     ($term->ReadLine ne "Term::ReadLine::Stub") ? "enabled" :
  127.         "available (try 'install Bundle::CPAN')";
  128.  
  129.     $CPAN::Frontend->myprint(
  130.                  sprintf qq{
  131. cpan shell -- CPAN exploration and modules installation (v%s%s)
  132. ReadLine support %s
  133.  
  134. },
  135.                              $CPAN::VERSION,
  136.                              $CPAN::Revision,
  137.                              $rl_avail
  138.                             )
  139.         unless $CPAN::Config->{'inhibit_startup_message'} ;
  140.     my($continuation) = "";
  141.   SHELLCOMMAND: while () {
  142.     if ($Suppress_readline) {
  143.         print $prompt;
  144.         last SHELLCOMMAND unless defined ($_ = <> );
  145.         chomp;
  146.     } else {
  147.         last SHELLCOMMAND unless
  148.                 defined ($_ = $term->readline($prompt, $commandline));
  149.     }
  150.     $_ = "$continuation$_" if $continuation;
  151.     s/^\s+//;
  152.     next SHELLCOMMAND if /^$/;
  153.     $_ = 'h' if /^\s*\?/;
  154.     if (/^(?:q(?:uit)?|bye|exit)$/i) {
  155.         last SHELLCOMMAND;
  156.     } elsif (s/\\$//s) {
  157.         chomp;
  158.         $continuation = $_;
  159.         $prompt = "    > ";
  160.     } elsif (/^\!/) {
  161.         s/^\!//;
  162.         my($eval) = $_;
  163.         package CPAN::Eval;
  164.         use vars qw($import_done);
  165.         CPAN->import(':DEFAULT') unless $import_done++;
  166.         CPAN->debug("eval[$eval]") if $CPAN::DEBUG;
  167.         eval($eval);
  168.         warn $@ if $@;
  169.         $continuation = "";
  170.         $prompt = $oprompt;
  171.     } elsif (/./) {
  172.         my(@line);
  173.         if ($] < 5.00322) { # parsewords had a bug until recently
  174.         @line = split;
  175.         } else {
  176.         eval { @line = Text::ParseWords::shellwords($_) };
  177.         warn($@), next SHELLCOMMAND if $@;
  178.                 warn("Text::Parsewords could not parse the line [$_]"),
  179.                     next SHELLCOMMAND unless @line;
  180.         }
  181.         $CPAN::META->debug("line[".join("|",@line)."]") if $CPAN::DEBUG;
  182.         my $command = shift @line;
  183.         eval { CPAN::Shell->$command(@line) };
  184.         warn $@ if $@;
  185.         chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  186.         $CPAN::Frontend->myprint("\n");
  187.         $continuation = "";
  188.         $prompt = $oprompt;
  189.     }
  190.     } continue {
  191.       $commandline = ""; # I do want to be able to pass a default to
  192.                          # shell, but on the second command I see no
  193.                          # use in that
  194.       $Signal=0;
  195.       CPAN::Queue->nullify_queue;
  196.       if ($try_detect_readline) {
  197.     if ($CPAN::META->has_inst("Term::ReadLine::Gnu")
  198.         ||
  199.         $CPAN::META->has_inst("Term::ReadLine::Perl")
  200.        ) {
  201.         delete $INC{"Term/ReadLine.pm"};
  202.         my $redef = 0;
  203.         local($SIG{__WARN__}) = CPAN::Shell::paintdots_onreload(\$redef);
  204.         require Term::ReadLine;
  205.         $CPAN::Frontend->myprint("\n$redef subroutines in ".
  206.                      "Term::ReadLine redefined\n");
  207.             @_ = ($oprompt,"");
  208.         goto &shell;
  209.     }
  210.       }
  211.     }
  212.     chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  213. }
  214.  
  215. package CPAN::CacheMgr;
  216. @CPAN::CacheMgr::ISA = qw(CPAN::InfoObj CPAN);
  217. use File::Find;
  218.  
  219. package CPAN::Config;
  220. use vars qw(%can $dot_cpan);
  221.  
  222. %can = (
  223.   'commit' => "Commit changes to disk",
  224.   'defaults' => "Reload defaults from disk",
  225.   'init'   => "Interactive setting of all options",
  226. );
  227.  
  228. package CPAN::FTP;
  229. use vars qw($Ua $Thesite $Themethod);
  230. @CPAN::FTP::ISA = qw(CPAN::Debug);
  231.  
  232. package CPAN::LWP::UserAgent;
  233. use vars qw(@ISA $USER $PASSWD $SETUPDONE);
  234. # we delay requiring LWP::UserAgent and setting up inheritence until we need it
  235.  
  236. package CPAN::Complete;
  237. @CPAN::Complete::ISA = qw(CPAN::Debug);
  238. @CPAN::Complete::COMMANDS = sort qw(
  239.                ! a b d h i m o q r u autobundle clean dump
  240.                make test install force readme reload look
  241.                        cvs_import ls
  242. ) unless @CPAN::Complete::COMMANDS;
  243.  
  244. package CPAN::Index;
  245. use vars qw($LAST_TIME $DATE_OF_02 $DATE_OF_03);
  246. @CPAN::Index::ISA = qw(CPAN::Debug);
  247. $LAST_TIME ||= 0;
  248. $DATE_OF_03 ||= 0;
  249. # use constant PROTOCOL => "2.0"; # outcommented to avoid warning on upgrade from 1.57
  250. sub PROTOCOL { 2.0 }
  251.  
  252. package CPAN::InfoObj;
  253. @CPAN::InfoObj::ISA = qw(CPAN::Debug);
  254.  
  255. package CPAN::Author;
  256. @CPAN::Author::ISA = qw(CPAN::InfoObj);
  257.  
  258. package CPAN::Distribution;
  259. @CPAN::Distribution::ISA = qw(CPAN::InfoObj);
  260.  
  261. package CPAN::Bundle;
  262. @CPAN::Bundle::ISA = qw(CPAN::Module);
  263.  
  264. package CPAN::Module;
  265. @CPAN::Module::ISA = qw(CPAN::InfoObj);
  266.  
  267. package CPAN::Shell;
  268. use vars qw($AUTOLOAD @ISA $COLOR_REGISTERED $ADVANCED_QUERY $PRINT_ORNAMENTING);
  269. @CPAN::Shell::ISA = qw(CPAN::Debug);
  270. $COLOR_REGISTERED ||= 0;
  271. $PRINT_ORNAMENTING ||= 0;
  272.  
  273. #-> sub CPAN::Shell::AUTOLOAD ;
  274. sub AUTOLOAD {
  275.     my($autoload) = $AUTOLOAD;
  276.     my $class = shift(@_);
  277.     # warn "autoload[$autoload] class[$class]";
  278.     $autoload =~ s/.*:://;
  279.     if ($autoload =~ /^w/) {
  280.     if ($CPAN::META->has_inst('CPAN::WAIT')) {
  281.         CPAN::WAIT->$autoload(@_);
  282.     } else {
  283.         $CPAN::Frontend->mywarn(qq{
  284. Commands starting with "w" require CPAN::WAIT to be installed.
  285. Please consider installing CPAN::WAIT to use the fulltext index.
  286. For this you just need to type
  287.     install CPAN::WAIT
  288. });
  289.     }
  290.     } else {
  291.     $CPAN::Frontend->mywarn(qq{Unknown command '$autoload'. }.
  292.                 qq{Type ? for help.
  293. });
  294.     }
  295. }
  296.  
  297. package CPAN::Tarzip;
  298. use vars qw($AUTOLOAD @ISA $BUGHUNTING);
  299. @CPAN::Tarzip::ISA = qw(CPAN::Debug);
  300. $BUGHUNTING = 0; # released code must have turned off
  301.  
  302. package CPAN::Queue;
  303.  
  304. # One use of the queue is to determine if we should or shouldn't
  305. # announce the availability of a new CPAN module
  306.  
  307. # Now we try to use it for dependency tracking. For that to happen
  308. # we need to draw a dependency tree and do the leaves first. This can
  309. # easily be reached by running CPAN.pm recursively, but we don't want
  310. # to waste memory and run into deep recursion. So what we can do is
  311. # this:
  312.  
  313. # CPAN::Queue is the package where the queue is maintained. Dependencies
  314. # often have high priority and must be brought to the head of the queue,
  315. # possibly by jumping the queue if they are already there. My first code
  316. # attempt tried to be extremely correct. Whenever a module needed
  317. # immediate treatment, I either unshifted it to the front of the queue,
  318. # or, if it was already in the queue, I spliced and let it bypass the
  319. # others. This became a too correct model that made it impossible to put
  320. # an item more than once into the queue. Why would you need that? Well,
  321. # you need temporary duplicates as the manager of the queue is a loop
  322. # that
  323. #
  324. #  (1) looks at the first item in the queue without shifting it off
  325. #
  326. #  (2) cares for the item
  327. #
  328. #  (3) removes the item from the queue, *even if its agenda failed and
  329. #      even if the item isn't the first in the queue anymore* (that way
  330. #      protecting against never ending queues)
  331. #
  332. # So if an item has prerequisites, the installation fails now, but we
  333. # want to retry later. That's easy if we have it twice in the queue.
  334. #
  335. # I also expect insane dependency situations where an item gets more
  336. # than two lives in the queue. Simplest example is triggered by 'install
  337. # Foo Foo Foo'. People make this kind of mistakes and I don't want to
  338. # get in the way. I wanted the queue manager to be a dumb servant, not
  339. # one that knows everything.
  340. #
  341. # Who would I tell in this model that the user wants to be asked before
  342. # processing? I can't attach that information to the module object,
  343. # because not modules are installed but distributions. So I'd have to
  344. # tell the distribution object that it should ask the user before
  345. # processing. Where would the question be triggered then? Most probably
  346. # in CPAN::Distribution::rematein.
  347. # Hope that makes sense, my head is a bit off:-) -- AK
  348.  
  349. use vars qw{ @All };
  350.  
  351. # CPAN::Queue::new ;
  352. sub new {
  353.   my($class,$s) = @_;
  354.   my $self = bless { qmod => $s }, $class;
  355.   push @All, $self;
  356.   return $self;
  357. }
  358.  
  359. # CPAN::Queue::first ;
  360. sub first {
  361.   my $obj = $All[0];
  362.   $obj->{qmod};
  363. }
  364.  
  365. # CPAN::Queue::delete_first ;
  366. sub delete_first {
  367.   my($class,$what) = @_;
  368.   my $i;
  369.   for my $i (0..$#All) {
  370.     if (  $All[$i]->{qmod} eq $what ) {
  371.       splice @All, $i, 1;
  372.       return;
  373.     }
  374.   }
  375. }
  376.  
  377. # CPAN::Queue::jumpqueue ;
  378. sub jumpqueue {
  379.     my $class = shift;
  380.     my @what = @_;
  381.     CPAN->debug(sprintf("before jumpqueue All[%s] what[%s]",
  382.                         join(",",map {$_->{qmod}} @All),
  383.                         join(",",@what)
  384.                        )) if $CPAN::DEBUG;
  385.   WHAT: for my $what (reverse @what) {
  386.         my $jumped = 0;
  387.         for (my $i=0; $i<$#All;$i++) { #prevent deep recursion
  388.             CPAN->debug("i[$All[$i]]what[$what]") if $CPAN::DEBUG;
  389.             if ($All[$i]->{qmod} eq $what){
  390.                 $jumped++;
  391.                 if ($jumped > 100) { # one's OK if e.g. just
  392.                                      # processing now; more are OK if
  393.                                      # user typed it several times
  394.                     $CPAN::Frontend->mywarn(
  395. qq{Object [$what] queued more than 100 times, ignoring}
  396.                  );
  397.                     next WHAT;
  398.                 }
  399.             }
  400.         }
  401.         my $obj = bless { qmod => $what }, $class;
  402.         unshift @All, $obj;
  403.     }
  404.     CPAN->debug(sprintf("after jumpqueue All[%s] what[%s]",
  405.                         join(",",map {$_->{qmod}} @All),
  406.                         join(",",@what)
  407.                        )) if $CPAN::DEBUG;
  408. }
  409.  
  410. # CPAN::Queue::exists ;
  411. sub exists {
  412.   my($self,$what) = @_;
  413.   my @all = map { $_->{qmod} } @All;
  414.   my $exists = grep { $_->{qmod} eq $what } @All;
  415.   # warn "in exists what[$what] all[@all] exists[$exists]";
  416.   $exists;
  417. }
  418.  
  419. # CPAN::Queue::delete ;
  420. sub delete {
  421.   my($self,$mod) = @_;
  422.   @All = grep { $_->{qmod} ne $mod } @All;
  423. }
  424.  
  425. # CPAN::Queue::nullify_queue ;
  426. sub nullify_queue {
  427.   @All = ();
  428. }
  429.  
  430.  
  431.  
  432. package CPAN;
  433.  
  434. $META ||= CPAN->new; # In case we re-eval ourselves we need the ||
  435.  
  436. # from here on only subs.
  437. ################################################################################
  438.  
  439. #-> sub CPAN::all_objects ;
  440. sub all_objects {
  441.     my($mgr,$class) = @_;
  442.     CPAN::Config->load unless $CPAN::Config_loaded++;
  443.     CPAN->debug("mgr[$mgr] class[$class]") if $CPAN::DEBUG;
  444.     CPAN::Index->reload;
  445.     values %{ $META->{readwrite}{$class} }; # unsafe meta access, ok
  446. }
  447. *all = \&all_objects;
  448.  
  449. # Called by shell, not in batch mode. In batch mode I see no risk in
  450. # having many processes updating something as installations are
  451. # continually checked at runtime. In shell mode I suspect it is
  452. # unintentional to open more than one shell at a time
  453.  
  454. #-> sub CPAN::checklock ;
  455. sub checklock {
  456.     my($self) = @_;
  457.     my $lockfile = MM->catfile($CPAN::Config->{cpan_home},".lock");
  458.     if (-f $lockfile && -M _ > 0) {
  459.     my $fh = FileHandle->new($lockfile) or
  460.             $CPAN::Frontend->mydie("Could not open $lockfile: $!");
  461.     my $other = <$fh>;
  462.     $fh->close;
  463.     if (defined $other && $other) {
  464.         chomp $other;
  465.         return if $$==$other; # should never happen
  466.         $CPAN::Frontend->mywarn(
  467.                     qq{
  468. There seems to be running another CPAN process ($other). Contacting...
  469. });
  470.         if (kill 0, $other) {
  471.         $CPAN::Frontend->mydie(qq{Other job is running.
  472. You may want to kill it and delete the lockfile, maybe. On UNIX try:
  473.     kill $other
  474.     rm $lockfile
  475. });
  476.         } elsif (-w $lockfile) {
  477.         my($ans) =
  478.             ExtUtils::MakeMaker::prompt
  479.             (qq{Other job not responding. Shall I overwrite }.
  480.              qq{the lockfile? (Y/N)},"y");
  481.         $CPAN::Frontend->myexit("Ok, bye\n")
  482.             unless $ans =~ /^y/i;
  483.         } else {
  484.         Carp::croak(
  485.                 qq{Lockfile $lockfile not writeable by you. }.
  486.                 qq{Cannot proceed.\n}.
  487.                 qq{    On UNIX try:\n}.
  488.                 qq{    rm $lockfile\n}.
  489.                 qq{  and then rerun us.\n}
  490.                );
  491.         }
  492.     } else {
  493.             $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: Lockfile $lockfile ".
  494.                                            "reports other process with ID ".
  495.                                            "$other. Cannot proceed.\n"));
  496.         }
  497.     }
  498.     my $dotcpan = $CPAN::Config->{cpan_home};
  499.     eval { File::Path::mkpath($dotcpan);};
  500.     if ($@) {
  501.       # A special case at least for Jarkko.
  502.       my $firsterror = $@;
  503.       my $seconderror;
  504.       my $symlinkcpan;
  505.       if (-l $dotcpan) {
  506.     $symlinkcpan = readlink $dotcpan;
  507.     die "readlink $dotcpan failed: $!" unless defined $symlinkcpan;
  508.     eval { File::Path::mkpath($symlinkcpan); };
  509.     if ($@) {
  510.       $seconderror = $@;
  511.     } else {
  512.       $CPAN::Frontend->mywarn(qq{
  513. Working directory $symlinkcpan created.
  514. });
  515.     }
  516.       }
  517.       unless (-d $dotcpan) {
  518.     my $diemess = qq{
  519. Your configuration suggests "$dotcpan" as your
  520. CPAN.pm working directory. I could not create this directory due
  521. to this error: $firsterror\n};
  522.     $diemess .= qq{
  523. As "$dotcpan" is a symlink to "$symlinkcpan",
  524. I tried to create that, but I failed with this error: $seconderror
  525. } if $seconderror;
  526.     $diemess .= qq{
  527. Please make sure the directory exists and is writable.
  528. };
  529.     $CPAN::Frontend->mydie($diemess);
  530.       }
  531.     }
  532.     my $fh;
  533.     unless ($fh = FileHandle->new(">$lockfile")) {
  534.     if ($! =~ /Permission/) {
  535.         my $incc = $INC{'CPAN/Config.pm'};
  536.         my $myincc = MM->catfile($ENV{HOME},'.cpan','CPAN','MyConfig.pm');
  537.         $CPAN::Frontend->myprint(qq{
  538.  
  539. Your configuration suggests that CPAN.pm should use a working
  540. directory of
  541.     $CPAN::Config->{cpan_home}
  542. Unfortunately we could not create the lock file
  543.     $lockfile
  544. due to permission problems.
  545.  
  546. Please make sure that the configuration variable
  547.     \$CPAN::Config->{cpan_home}
  548. points to a directory where you can write a .lock file. You can set
  549. this variable in either
  550.     $incc
  551. or
  552.     $myincc
  553.  
  554. });
  555.     }
  556.     $CPAN::Frontend->mydie("Could not open >$lockfile: $!");
  557.     }
  558.     $fh->print($$, "\n");
  559.     $self->{LOCK} = $lockfile;
  560.     $fh->close;
  561.     $SIG{TERM} = sub {
  562.       &cleanup;
  563.       $CPAN::Frontend->mydie("Got SIGTERM, leaving");
  564.     };
  565.     $SIG{INT} = sub {
  566.       # no blocks!!!
  567.       &cleanup if $Signal;
  568.       $CPAN::Frontend->mydie("Got another SIGINT") if $Signal;
  569.       print "Caught SIGINT\n";
  570.       $Signal++;
  571.     };
  572.  
  573. #       From: Larry Wall <larry@wall.org>
  574. #       Subject: Re: deprecating SIGDIE
  575. #       To: perl5-porters@perl.org
  576. #       Date: Thu, 30 Sep 1999 14:58:40 -0700 (PDT)
  577. #
  578. #       The original intent of __DIE__ was only to allow you to substitute one
  579. #       kind of death for another on an application-wide basis without respect
  580. #       to whether you were in an eval or not.  As a global backstop, it should
  581. #       not be used any more lightly (or any more heavily :-) than class
  582. #       UNIVERSAL.  Any attempt to build a general exception model on it should
  583. #       be politely squashed.  Any bug that causes every eval {} to have to be
  584. #       modified should be not so politely squashed.
  585. #
  586. #       Those are my current opinions.  It is also my optinion that polite
  587. #       arguments degenerate to personal arguments far too frequently, and that
  588. #       when they do, it's because both people wanted it to, or at least didn't
  589. #       sufficiently want it not to.
  590. #
  591. #       Larry
  592.  
  593.     # global backstop to cleanup if we should really die
  594.     $SIG{__DIE__} = \&cleanup;
  595.     $self->debug("Signal handler set.") if $CPAN::DEBUG;
  596. }
  597.  
  598. #-> sub CPAN::DESTROY ;
  599. sub DESTROY {
  600.     &cleanup; # need an eval?
  601. }
  602.  
  603. #-> sub CPAN::anycwd ;
  604. sub anycwd () {
  605.     my $getcwd;
  606.     $getcwd = $CPAN::Config->{'getcwd'} || 'cwd';
  607.     CPAN->$getcwd();
  608. }
  609.  
  610. #-> sub CPAN::cwd ;
  611. sub cwd {Cwd::cwd();}
  612.  
  613. #-> sub CPAN::getcwd ;
  614. sub getcwd {Cwd::getcwd();}
  615.  
  616. #-> sub CPAN::exists ;
  617. sub exists {
  618.     my($mgr,$class,$id) = @_;
  619.     CPAN::Config->load unless $CPAN::Config_loaded++;
  620.     CPAN::Index->reload;
  621.     ### Carp::croak "exists called without class argument" unless $class;
  622.     $id ||= "";
  623.     exists $META->{readonly}{$class}{$id} or
  624.         exists $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  625. }
  626.  
  627. #-> sub CPAN::delete ;
  628. sub delete {
  629.   my($mgr,$class,$id) = @_;
  630.   delete $META->{readonly}{$class}{$id}; # unsafe meta access, ok
  631.   delete $META->{readwrite}{$class}{$id}; # unsafe meta access, ok
  632. }
  633.  
  634. #-> sub CPAN::has_usable
  635. # has_inst is sometimes too optimistic, we should replace it with this
  636. # has_usable whenever a case is given
  637. sub has_usable {
  638.     my($self,$mod,$message) = @_;
  639.     return 1 if $HAS_USABLE->{$mod};
  640.     my $has_inst = $self->has_inst($mod,$message);
  641.     return unless $has_inst;
  642.     my $usable;
  643.     $usable = {
  644.                LWP => [ # we frequently had "Can't locate object
  645.                         # method "new" via package "LWP::UserAgent" at
  646.                         # (eval 69) line 2006
  647.                        sub {require LWP},
  648.                        sub {require LWP::UserAgent},
  649.                        sub {require HTTP::Request},
  650.                        sub {require URI::URL},
  651.                       ],
  652.                Net::FTP => [
  653.                             sub {require Net::FTP},
  654.                             sub {require Net::Config},
  655.                            ]
  656.               };
  657.     if ($usable->{$mod}) {
  658.       for my $c (0..$#{$usable->{$mod}}) {
  659.         my $code = $usable->{$mod}[$c];
  660.         my $ret = eval { &$code() };
  661.         if ($@) {
  662.           warn "DEBUG: c[$c]\$\@[$@]ret[$ret]";
  663.           return;
  664.         }
  665.       }
  666.     }
  667.     return $HAS_USABLE->{$mod} = 1;
  668. }
  669.  
  670. #-> sub CPAN::has_inst
  671. sub has_inst {
  672.     my($self,$mod,$message) = @_;
  673.     Carp::croak("CPAN->has_inst() called without an argument")
  674.     unless defined $mod;
  675.     if (defined $message && $message eq "no"
  676.         ||
  677.         exists $CPAN::META->{dontload_hash}{$mod} # unsafe meta access, ok
  678.         ||
  679.         exists $CPAN::Config->{dontload_hash}{$mod}
  680.        ) {
  681.       $CPAN::META->{dontload_hash}{$mod}||=1; # unsafe meta access, ok
  682.       return 0;
  683.     }
  684.     my $file = $mod;
  685.     my $obj;
  686.     $file =~ s|::|/|g;
  687.     $file =~ s|/|\\|g if $^O eq 'MSWin32';
  688.     $file .= ".pm";
  689.     if ($INC{$file}) {
  690.     # checking %INC is wrong, because $INC{LWP} may be true
  691.     # although $INC{"URI/URL.pm"} may have failed. But as
  692.     # I really want to say "bla loaded OK", I have to somehow
  693.     # cache results.
  694.     ### warn "$file in %INC"; #debug
  695.     return 1;
  696.     } elsif (eval { require $file }) {
  697.     # eval is good: if we haven't yet read the database it's
  698.     # perfect and if we have installed the module in the meantime,
  699.     # it tries again. The second require is only a NOOP returning
  700.     # 1 if we had success, otherwise it's retrying
  701.  
  702.     $CPAN::Frontend->myprint("CPAN: $mod loaded ok\n");
  703.     if ($mod eq "CPAN::WAIT") {
  704.         push @CPAN::Shell::ISA, CPAN::WAIT;
  705.     }
  706.     return 1;
  707.     } elsif ($mod eq "Net::FTP") {
  708.     $CPAN::Frontend->mywarn(qq{
  709.   Please, install Net::FTP as soon as possible. CPAN.pm installs it for you
  710.   if you just type
  711.       install Bundle::libnet
  712.  
  713. }) unless $Have_warned->{"Net::FTP"}++;
  714.     sleep 3;
  715.     } elsif ($mod eq "MD5"){
  716.     $CPAN::Frontend->myprint(qq{
  717.   CPAN: MD5 security checks disabled because MD5 not installed.
  718.   Please consider installing the MD5 module.
  719.  
  720. });
  721.     sleep 2;
  722.     } else {
  723.     delete $INC{$file}; # if it inc'd LWP but failed during, say, URI
  724.     }
  725.     return 0;
  726. }
  727.  
  728. #-> sub CPAN::instance ;
  729. sub instance {
  730.     my($mgr,$class,$id) = @_;
  731.     CPAN::Index->reload;
  732.     $id ||= "";
  733.     # unsafe meta access, ok?
  734.     return $META->{readwrite}{$class}{$id} if exists $META->{readwrite}{$class}{$id};
  735.     $META->{readwrite}{$class}{$id} ||= $class->new(ID => $id);
  736. }
  737.  
  738. #-> sub CPAN::new ;
  739. sub new {
  740.     bless {}, shift;
  741. }
  742.  
  743. #-> sub CPAN::cleanup ;
  744. sub cleanup {
  745.   # warn "cleanup called with arg[@_] End[$End] Signal[$Signal]";
  746.   local $SIG{__DIE__} = '';
  747.   my($message) = @_;
  748.   my $i = 0;
  749.   my $ineval = 0;
  750.   if (
  751.       0 &&           # disabled, try reload cpan with it
  752.       $] > 5.004_60  # thereabouts
  753.      ) {
  754.     $ineval = $^S;
  755.   } else {
  756.     my($subroutine);
  757.     while ((undef,undef,undef,$subroutine) = caller(++$i)) {
  758.       $ineval = 1, last if
  759.       $subroutine eq '(eval)';
  760.     }
  761.   }
  762.   return if $ineval && !$End;
  763.   return unless defined $META->{LOCK}; # unsafe meta access, ok
  764.   return unless -f $META->{LOCK}; # unsafe meta access, ok
  765.   unlink $META->{LOCK}; # unsafe meta access, ok
  766.   # require Carp;
  767.   # Carp::cluck("DEBUGGING");
  768.   $CPAN::Frontend->mywarn("Lockfile removed.\n");
  769. }
  770.  
  771. package CPAN::CacheMgr;
  772.  
  773. #-> sub CPAN::CacheMgr::as_string ;
  774. sub as_string {
  775.     eval { require Data::Dumper };
  776.     if ($@) {
  777.     return shift->SUPER::as_string;
  778.     } else {
  779.     return Data::Dumper::Dumper(shift);
  780.     }
  781. }
  782.  
  783. #-> sub CPAN::CacheMgr::cachesize ;
  784. sub cachesize {
  785.     shift->{DU};
  786. }
  787.  
  788. #-> sub CPAN::CacheMgr::tidyup ;
  789. sub tidyup {
  790.   my($self) = @_;
  791.   return unless -d $self->{ID};
  792.   while ($self->{DU} > $self->{'MAX'} ) {
  793.     my($toremove) = shift @{$self->{FIFO}};
  794.     $CPAN::Frontend->myprint(sprintf(
  795.                      "Deleting from cache".
  796.                      ": $toremove (%.1f>%.1f MB)\n",
  797.                      $self->{DU}, $self->{'MAX'})
  798.                 );
  799.     return if $CPAN::Signal;
  800.     $self->force_clean_cache($toremove);
  801.     return if $CPAN::Signal;
  802.   }
  803. }
  804.  
  805. #-> sub CPAN::CacheMgr::dir ;
  806. sub dir {
  807.     shift->{ID};
  808. }
  809.  
  810. #-> sub CPAN::CacheMgr::entries ;
  811. sub entries {
  812.     my($self,$dir) = @_;
  813.     return unless defined $dir;
  814.     $self->debug("reading dir[$dir]") if $CPAN::DEBUG;
  815.     $dir ||= $self->{ID};
  816.     my($cwd) = CPAN::anycwd();
  817.     chdir $dir or Carp::croak("Can't chdir to $dir: $!");
  818.     my $dh = DirHandle->new(File::Spec->curdir)
  819.         or Carp::croak("Couldn't opendir $dir: $!");
  820.     my(@entries);
  821.     for ($dh->read) {
  822.     next if $_ eq "." || $_ eq "..";
  823.     if (-f $_) {
  824.         push @entries, MM->catfile($dir,$_);
  825.     } elsif (-d _) {
  826.         push @entries, MM->catdir($dir,$_);
  827.     } else {
  828.         $CPAN::Frontend->mywarn("Warning: weird direntry in $dir: $_\n");
  829.     }
  830.     }
  831.     chdir $cwd or Carp::croak("Can't chdir to $cwd: $!");
  832.     sort { -M $b <=> -M $a} @entries;
  833. }
  834.  
  835. #-> sub CPAN::CacheMgr::disk_usage ;
  836. sub disk_usage {
  837.     my($self,$dir) = @_;
  838.     return if exists $self->{SIZE}{$dir};
  839.     return if $CPAN::Signal;
  840.     my($Du) = 0;
  841.     find(
  842.      sub {
  843.        $File::Find::prune++ if $CPAN::Signal;
  844.        return if -l $_;
  845.        if ($^O eq 'MacOS') {
  846.          require Mac::Files;
  847.          my $cat  = Mac::Files::FSpGetCatInfo($_);
  848.          $Du += $cat->ioFlLgLen() + $cat->ioFlRLgLen() if $cat;
  849.        } else {
  850.          $Du += (-s _);
  851.        }
  852.      },
  853.      $dir
  854.     );
  855.     return if $CPAN::Signal;
  856.     $self->{SIZE}{$dir} = $Du/1024/1024;
  857.     push @{$self->{FIFO}}, $dir;
  858.     $self->debug("measured $dir is $Du") if $CPAN::DEBUG;
  859.     $self->{DU} += $Du/1024/1024;
  860.     $self->{DU};
  861. }
  862.  
  863. #-> sub CPAN::CacheMgr::force_clean_cache ;
  864. sub force_clean_cache {
  865.     my($self,$dir) = @_;
  866.     return unless -e $dir;
  867.     $self->debug("have to rmtree $dir, will free $self->{SIZE}{$dir}")
  868.     if $CPAN::DEBUG;
  869.     File::Path::rmtree($dir);
  870.     $self->{DU} -= $self->{SIZE}{$dir};
  871.     delete $self->{SIZE}{$dir};
  872. }
  873.  
  874. #-> sub CPAN::CacheMgr::new ;
  875. sub new {
  876.     my $class = shift;
  877.     my $time = time;
  878.     my($debug,$t2);
  879.     $debug = "";
  880.     my $self = {
  881.         ID => $CPAN::Config->{'build_dir'},
  882.         MAX => $CPAN::Config->{'build_cache'},
  883.         SCAN => $CPAN::Config->{'scan_cache'} || 'atstart',
  884.         DU => 0
  885.            };
  886.     File::Path::mkpath($self->{ID});
  887.     my $dh = DirHandle->new($self->{ID});
  888.     bless $self, $class;
  889.     $self->scan_cache;
  890.     $t2 = time;
  891.     $debug .= "timing of CacheMgr->new: ".($t2 - $time);
  892.     $time = $t2;
  893.     CPAN->debug($debug) if $CPAN::DEBUG;
  894.     $self;
  895. }
  896.  
  897. #-> sub CPAN::CacheMgr::scan_cache ;
  898. sub scan_cache {
  899.     my $self = shift;
  900.     return if $self->{SCAN} eq 'never';
  901.     $CPAN::Frontend->mydie("Unknown scan_cache argument: $self->{SCAN}")
  902.     unless $self->{SCAN} eq 'atstart';
  903.     $CPAN::Frontend->myprint(
  904.                  sprintf("Scanning cache %s for sizes\n",
  905.                      $self->{ID}));
  906.     my $e;
  907.     for $e ($self->entries($self->{ID})) {
  908.     next if $e eq ".." || $e eq ".";
  909.     $self->disk_usage($e);
  910.     return if $CPAN::Signal;
  911.     }
  912.     $self->tidyup;
  913. }
  914.  
  915. package CPAN::Debug;
  916.  
  917. #-> sub CPAN::Debug::debug ;
  918. sub debug {
  919.     my($self,$arg) = @_;
  920.     my($caller,$func,$line,@rest) = caller(1); # caller(0) eg
  921.                                                # Complete, caller(1)
  922.                                                # eg readline
  923.     ($caller) = caller(0);
  924.     $caller =~ s/.*:://;
  925.     $arg = "" unless defined $arg;
  926.     my $rest = join "|", map { defined $_ ? $_ : "UNDEF" } @rest;
  927.     if ($CPAN::DEBUG{$caller} & $CPAN::DEBUG){
  928.     if ($arg and ref $arg) {
  929.         eval { require Data::Dumper };
  930.         if ($@) {
  931.         $CPAN::Frontend->myprint($arg->as_string);
  932.         } else {
  933.         $CPAN::Frontend->myprint(Data::Dumper::Dumper($arg));
  934.         }
  935.     } else {
  936.         $CPAN::Frontend->myprint("Debug($caller:$func,$line,[$rest]): $arg\n");
  937.     }
  938.     }
  939. }
  940.  
  941. package CPAN::Config;
  942.  
  943. #-> sub CPAN::Config::edit ;
  944. # returns true on successful action
  945. sub edit {
  946.     my($self,@args) = @_;
  947.     return unless @args;
  948.     CPAN->debug("self[$self]args[".join(" | ",@args)."]");
  949.     my($o,$str,$func,$args,$key_exists);
  950.     $o = shift @args;
  951.     if($can{$o}) {
  952.     $self->$o(@args);
  953.     return 1;
  954.     } else {
  955.         CPAN->debug("o[$o]") if $CPAN::DEBUG;
  956.     if ($o =~ /list$/) {
  957.         $func = shift @args;
  958.         $func ||= "";
  959.             CPAN->debug("func[$func]") if $CPAN::DEBUG;
  960.             my $changed;
  961.         # Let's avoid eval, it's easier to comprehend without.
  962.         if ($func eq "push") {
  963.         push @{$CPAN::Config->{$o}}, @args;
  964.                 $changed = 1;
  965.         } elsif ($func eq "pop") {
  966.         pop @{$CPAN::Config->{$o}};
  967.                 $changed = 1;
  968.         } elsif ($func eq "shift") {
  969.         shift @{$CPAN::Config->{$o}};
  970.                 $changed = 1;
  971.         } elsif ($func eq "unshift") {
  972.         unshift @{$CPAN::Config->{$o}}, @args;
  973.                 $changed = 1;
  974.         } elsif ($func eq "splice") {
  975.         splice @{$CPAN::Config->{$o}}, @args;
  976.                 $changed = 1;
  977.         } elsif (@args) {
  978.         $CPAN::Config->{$o} = [@args];
  979.                 $changed = 1;
  980.         } else {
  981.                 $self->prettyprint($o);
  982.         }
  983.             if ($o eq "urllist" && $changed) {
  984.                 # reset the cached values
  985.                 undef $CPAN::FTP::Thesite;
  986.                 undef $CPAN::FTP::Themethod;
  987.             }
  988.             return $changed;
  989.     } else {
  990.         $CPAN::Config->{$o} = $args[0] if defined $args[0];
  991.         $self->prettyprint($o);
  992.     }
  993.     }
  994. }
  995.  
  996. sub prettyprint {
  997.   my($self,$k) = @_;
  998.   my $v = $CPAN::Config->{$k};
  999.   if (ref $v) {
  1000.     my(@report) = ref $v eq "ARRAY" ?
  1001.         @$v :
  1002.             map { sprintf("   %-18s => %s\n",
  1003.                           $_,
  1004.                           defined $v->{$_} ? $v->{$_} : "UNDEFINED"
  1005.                          )} keys %$v;
  1006.     $CPAN::Frontend->myprint(
  1007.                              join(
  1008.                                   "",
  1009.                                   sprintf(
  1010.                                           "    %-18s\n",
  1011.                                           $k
  1012.                                          ),
  1013.                                   map {"\t$_\n"} @report
  1014.                                  )
  1015.                             );
  1016.   } elsif (defined $v) {
  1017.     $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, $v);
  1018.   } else {
  1019.     $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, "UNDEFINED");
  1020.   }
  1021. }
  1022.  
  1023. #-> sub CPAN::Config::commit ;
  1024. sub commit {
  1025.     my($self,$configpm) = @_;
  1026.     unless (defined $configpm){
  1027.     $configpm ||= $INC{"CPAN/MyConfig.pm"};
  1028.     $configpm ||= $INC{"CPAN/Config.pm"};
  1029.     $configpm || Carp::confess(q{
  1030. CPAN::Config::commit called without an argument.
  1031. Please specify a filename where to save the configuration or try
  1032. "o conf init" to have an interactive course through configing.
  1033. });
  1034.     }
  1035.     my($mode);
  1036.     if (-f $configpm) {
  1037.     $mode = (stat $configpm)[2];
  1038.     if ($mode && ! -w _) {
  1039.         Carp::confess("$configpm is not writable");
  1040.     }
  1041.     }
  1042.  
  1043.     my $msg;
  1044.     $msg = <<EOF unless $configpm =~ /MyConfig/;
  1045.  
  1046. # This is CPAN.pm's systemwide configuration file. This file provides
  1047. # defaults for users, and the values can be changed in a per-user
  1048. # configuration file. The user-config file is being looked for as
  1049. # ~/.cpan/CPAN/MyConfig.pm.
  1050.  
  1051. EOF
  1052.     $msg ||= "\n";
  1053.     my($fh) = FileHandle->new;
  1054.     rename $configpm, "$configpm~" if -f $configpm;
  1055.     open $fh, ">$configpm" or
  1056.         $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
  1057.     $fh->print(qq[$msg\$CPAN::Config = \{\n]);
  1058.     foreach (sort keys %$CPAN::Config) {
  1059.     $fh->print(
  1060.            "  '$_' => ",
  1061.            ExtUtils::MakeMaker::neatvalue($CPAN::Config->{$_}),
  1062.            ",\n"
  1063.           );
  1064.     }
  1065.  
  1066.     $fh->print("};\n1;\n__END__\n");
  1067.     close $fh;
  1068.  
  1069.     #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
  1070.     #chmod $mode, $configpm;
  1071. ###why was that so?    $self->defaults;
  1072.     $CPAN::Frontend->myprint("commit: wrote $configpm\n");
  1073.     1;
  1074. }
  1075.  
  1076. *default = \&defaults;
  1077. #-> sub CPAN::Config::defaults ;
  1078. sub defaults {
  1079.     my($self) = @_;
  1080.     $self->unload;
  1081.     $self->load;
  1082.     1;
  1083. }
  1084.  
  1085. sub init {
  1086.     my($self) = @_;
  1087.     undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
  1088.                                                       # have the least
  1089.                                                       # important
  1090.                                                       # variable
  1091.                                                       # undefined
  1092.     $self->load;
  1093.     1;
  1094. }
  1095.  
  1096. #-> sub CPAN::Config::load ;
  1097. sub load {
  1098.     my($self) = shift;
  1099.     my(@miss);
  1100.     use Carp;
  1101.     eval {require CPAN::Config;};       # We eval because of some
  1102.                                         # MakeMaker problems
  1103.     unless ($dot_cpan++){
  1104.       unshift @INC, MM->catdir($ENV{HOME},".cpan");
  1105.       eval {require CPAN::MyConfig;};   # where you can override
  1106.                                         # system wide settings
  1107.       shift @INC;
  1108.     }
  1109.     return unless @miss = $self->missing_config_data;
  1110.  
  1111.     require CPAN::FirstTime;
  1112.     my($configpm,$fh,$redo,$theycalled);
  1113.     $redo ||= "";
  1114.     $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
  1115.     if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
  1116.     $configpm = $INC{"CPAN/Config.pm"};
  1117.     $redo++;
  1118.     } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
  1119.     $configpm = $INC{"CPAN/MyConfig.pm"};
  1120.     $redo++;
  1121.     } else {
  1122.     my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
  1123.     my($configpmdir) = MM->catdir($path_to_cpan,"CPAN");
  1124.     my($configpmtest) = MM->catfile($configpmdir,"Config.pm");
  1125.     if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
  1126.         if (-w $configpmtest) {
  1127.         $configpm = $configpmtest;
  1128.         } elsif (-w $configpmdir) {
  1129.         #_#_# following code dumped core on me with 5.003_11, a.k.
  1130.         unlink "$configpmtest.bak" if -f "$configpmtest.bak";
  1131.         rename $configpmtest, "$configpmtest.bak" if -f $configpmtest;
  1132.         my $fh = FileHandle->new;
  1133.         if ($fh->open(">$configpmtest")) {
  1134.             $fh->print("1;\n");
  1135.             $configpm = $configpmtest;
  1136.         } else {
  1137.             # Should never happen
  1138.             Carp::confess("Cannot open >$configpmtest");
  1139.         }
  1140.         }
  1141.     }
  1142.     unless ($configpm) {
  1143.         $configpmdir = MM->catdir($ENV{HOME},".cpan","CPAN");
  1144.         File::Path::mkpath($configpmdir);
  1145.         $configpmtest = MM->catfile($configpmdir,"MyConfig.pm");
  1146.         if (-w $configpmtest) {
  1147.         $configpm = $configpmtest;
  1148.         } elsif (-w $configpmdir) {
  1149.         #_#_# following code dumped core on me with 5.003_11, a.k.
  1150.         my $fh = FileHandle->new;
  1151.         if ($fh->open(">$configpmtest")) {
  1152.             $fh->print("1;\n");
  1153.             $configpm = $configpmtest;
  1154.         } else {
  1155.             # Should never happen
  1156.             Carp::confess("Cannot open >$configpmtest");
  1157.         }
  1158.         } else {
  1159.         Carp::confess(qq{WARNING: CPAN.pm is unable to }.
  1160.                   qq{create a configuration file.});
  1161.         }
  1162.     }
  1163.     }
  1164.     local($") = ", ";
  1165.     $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
  1166. We have to reconfigure CPAN.pm due to following uninitialized parameters:
  1167.  
  1168. @miss
  1169. END
  1170.     $CPAN::Frontend->myprint(qq{
  1171. $configpm initialized.
  1172. });
  1173.     sleep 2;
  1174.     CPAN::FirstTime::init($configpm);
  1175. }
  1176.  
  1177. #-> sub CPAN::Config::missing_config_data ;
  1178. sub missing_config_data {
  1179.     my(@miss);
  1180.     for (
  1181.          "cpan_home", "keep_source_where", "build_dir", "build_cache",
  1182.          "scan_cache", "index_expire", "gzip", "tar", "unzip", "make",
  1183.          "pager",
  1184.          "makepl_arg", "make_arg", "make_install_arg", "urllist",
  1185.          "inhibit_startup_message", "ftp_proxy", "http_proxy", "no_proxy",
  1186.          "prerequisites_policy",
  1187.          "cache_metadata",
  1188.         ) {
  1189.     push @miss, $_ unless defined $CPAN::Config->{$_};
  1190.     }
  1191.     return @miss;
  1192. }
  1193.  
  1194. #-> sub CPAN::Config::unload ;
  1195. sub unload {
  1196.     delete $INC{'CPAN/MyConfig.pm'};
  1197.     delete $INC{'CPAN/Config.pm'};
  1198. }
  1199.  
  1200. #-> sub CPAN::Config::help ;
  1201. sub help {
  1202.     $CPAN::Frontend->myprint(q[
  1203. Known options:
  1204.   defaults  reload default config values from disk
  1205.   commit    commit session changes to disk
  1206.   init      go through a dialog to set all parameters
  1207.  
  1208. You may edit key values in the follow fashion (the "o" is a literal
  1209. letter o):
  1210.  
  1211.   o conf build_cache 15
  1212.  
  1213.   o conf build_dir "/foo/bar"
  1214.  
  1215.   o conf urllist shift
  1216.  
  1217.   o conf urllist unshift ftp://ftp.foo.bar/
  1218.  
  1219. ]);
  1220.     undef; #don't reprint CPAN::Config
  1221. }
  1222.  
  1223. #-> sub CPAN::Config::cpl ;
  1224. sub cpl {
  1225.     my($word,$line,$pos) = @_;
  1226.     $word ||= "";
  1227.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  1228.     my(@words) = split " ", substr($line,0,$pos+1);
  1229.     if (
  1230.     defined($words[2])
  1231.     and
  1232.     (
  1233.      $words[2] =~ /list$/ && @words == 3
  1234.      ||
  1235.      $words[2] =~ /list$/ && @words == 4 && length($word)
  1236.     )
  1237.        ) {
  1238.     return grep /^\Q$word\E/, qw(splice shift unshift pop push);
  1239.     } elsif (@words >= 4) {
  1240.     return ();
  1241.     }
  1242.     my(@o_conf) = (keys %CPAN::Config::can, keys %$CPAN::Config);
  1243.     return grep /^\Q$word\E/, @o_conf;
  1244. }
  1245.  
  1246. package CPAN::Shell;
  1247.  
  1248. #-> sub CPAN::Shell::h ;
  1249. sub h {
  1250.     my($class,$about) = @_;
  1251.     if (defined $about) {
  1252.     $CPAN::Frontend->myprint("Detailed help not yet implemented\n");
  1253.     } else {
  1254.     $CPAN::Frontend->myprint(q{
  1255. Display Information
  1256.  command  argument          description
  1257.  a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
  1258.  i        WORD or /REGEXP/  about anything of above
  1259.  r        NONE              reinstall recommendations
  1260.  ls       AUTHOR            about files in the author's directory
  1261.  
  1262. Download, Test, Make, Install...
  1263.  get                        download
  1264.  make                       make (implies get)
  1265.  test      MODULES,         make test (implies make)
  1266.  install   DISTS, BUNDLES   make install (implies test)
  1267.  clean                      make clean
  1268.  look                       open subshell in these dists' directories
  1269.  readme                     display these dists' README files
  1270.  
  1271. Other
  1272.  h,?           display this menu       ! perl-code   eval a perl command
  1273.  o conf [opt]  set and query options   q             quit the cpan shell
  1274.  reload cpan   load CPAN.pm again      reload index  load newer indices
  1275.  autobundle    Snapshot                force cmd     unconditionally do cmd});
  1276.     }
  1277. }
  1278.  
  1279. *help = \&h;
  1280.  
  1281. #-> sub CPAN::Shell::a ;
  1282. sub a {
  1283.   my($self,@arg) = @_;
  1284.   # authors are always UPPERCASE
  1285.   for (@arg) {
  1286.     $_ = uc $_ unless /=/;
  1287.   }
  1288.   $CPAN::Frontend->myprint($self->format_result('Author',@arg));
  1289. }
  1290.  
  1291. #-> sub CPAN::Shell::ls ;
  1292. sub ls      {
  1293.     my($self,@arg) = @_;
  1294.     my @accept;
  1295.     for (@arg) {
  1296.         unless (/^[A-Z\-]+$/i) {
  1297.             $CPAN::Frontend->mywarn("ls command rejects argument $_: not an author");
  1298.             next;
  1299.         }
  1300.         push @accept, uc $_;
  1301.     }
  1302.     for my $a (@accept){
  1303.         my $author = $self->expand('Author',$a) or die "No author found for $a";
  1304.         $author->ls;
  1305.     }
  1306. }
  1307.  
  1308. #-> sub CPAN::Shell::local_bundles ;
  1309. sub local_bundles {
  1310.     my($self,@which) = @_;
  1311.     my($incdir,$bdir,$dh);
  1312.     foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  1313.         my @bbase = "Bundle";
  1314.         while (my $bbase = shift @bbase) {
  1315.             $bdir = MM->catdir($incdir,split /::/, $bbase);
  1316.             CPAN->debug("bdir[$bdir]\@bbase[@bbase]") if $CPAN::DEBUG;
  1317.             if ($dh = DirHandle->new($bdir)) { # may fail
  1318.                 my($entry);
  1319.                 for $entry ($dh->read) {
  1320.                     next if $entry =~ /^\./;
  1321.                     if (-d MM->catdir($bdir,$entry)){
  1322.                         push @bbase, "$bbase\::$entry";
  1323.                     } else {
  1324.                         next unless $entry =~ s/\.pm(?!\n)\Z//;
  1325.                         $CPAN::META->instance('CPAN::Bundle',"$bbase\::$entry");
  1326.                     }
  1327.                 }
  1328.             }
  1329.         }
  1330.     }
  1331. }
  1332.  
  1333. #-> sub CPAN::Shell::b ;
  1334. sub b {
  1335.     my($self,@which) = @_;
  1336.     CPAN->debug("which[@which]") if $CPAN::DEBUG;
  1337.     $self->local_bundles;
  1338.     $CPAN::Frontend->myprint($self->format_result('Bundle',@which));
  1339. }
  1340.  
  1341. #-> sub CPAN::Shell::d ;
  1342. sub d { $CPAN::Frontend->myprint(shift->format_result('Distribution',@_));}
  1343.  
  1344. #-> sub CPAN::Shell::m ;
  1345. sub m { # emacs confused here }; sub mimimimimi { # emacs in sync here
  1346.     $CPAN::Frontend->myprint(shift->format_result('Module',@_));
  1347. }
  1348.  
  1349. #-> sub CPAN::Shell::i ;
  1350. sub i {
  1351.     my($self) = shift;
  1352.     my(@args) = @_;
  1353.     my(@type,$type,@m);
  1354.     @type = qw/Author Bundle Distribution Module/;
  1355.     @args = '/./' unless @args;
  1356.     my(@result);
  1357.     for $type (@type) {
  1358.     push @result, $self->expand($type,@args);
  1359.     }
  1360.     my $result = @result == 1 ?
  1361.     $result[0]->as_string :
  1362.             @result == 0 ?
  1363.                 "No objects found of any type for argument @args\n" :
  1364.                     join("",
  1365.                          (map {$_->as_glimpse} @result),
  1366.                          scalar @result, " items found\n",
  1367.                         );
  1368.     $CPAN::Frontend->myprint($result);
  1369. }
  1370.  
  1371. #-> sub CPAN::Shell::o ;
  1372.  
  1373. # CPAN::Shell::o and CPAN::Config::edit are closely related. 'o conf'
  1374. # should have been called set and 'o debug' maybe 'set debug'
  1375. sub o {
  1376.     my($self,$o_type,@o_what) = @_;
  1377.     $o_type ||= "";
  1378.     CPAN->debug("o_type[$o_type] o_what[".join(" | ",@o_what)."]\n");
  1379.     if ($o_type eq 'conf') {
  1380.     shift @o_what if @o_what && $o_what[0] eq 'help';
  1381.     if (!@o_what) { # print all things, "o conf"
  1382.         my($k,$v);
  1383.         $CPAN::Frontend->myprint("CPAN::Config options");
  1384.         if (exists $INC{'CPAN/Config.pm'}) {
  1385.           $CPAN::Frontend->myprint(" from $INC{'CPAN/Config.pm'}");
  1386.         }
  1387.         if (exists $INC{'CPAN/MyConfig.pm'}) {
  1388.           $CPAN::Frontend->myprint(" and $INC{'CPAN/MyConfig.pm'}");
  1389.         }
  1390.         $CPAN::Frontend->myprint(":\n");
  1391.         for $k (sort keys %CPAN::Config::can) {
  1392.         $v = $CPAN::Config::can{$k};
  1393.         $CPAN::Frontend->myprint(sprintf "    %-18s %s\n", $k, $v);
  1394.         }
  1395.         $CPAN::Frontend->myprint("\n");
  1396.         for $k (sort keys %$CPAN::Config) {
  1397.                 CPAN::Config->prettyprint($k);
  1398.         }
  1399.         $CPAN::Frontend->myprint("\n");
  1400.     } elsif (!CPAN::Config->edit(@o_what)) {
  1401.         $CPAN::Frontend->myprint(qq{Type 'o conf' to view configuration }.
  1402.                                      qq{edit options\n\n});
  1403.     }
  1404.     } elsif ($o_type eq 'debug') {
  1405.     my(%valid);
  1406.     @o_what = () if defined $o_what[0] && $o_what[0] =~ /help/i;
  1407.     if (@o_what) {
  1408.         while (@o_what) {
  1409.         my($what) = shift @o_what;
  1410.                 if ($what =~ s/^-// && exists $CPAN::DEBUG{$what}) {
  1411.                     $CPAN::DEBUG &= $CPAN::DEBUG ^ $CPAN::DEBUG{$what};
  1412.                     next;
  1413.                 }
  1414.         if ( exists $CPAN::DEBUG{$what} ) {
  1415.             $CPAN::DEBUG |= $CPAN::DEBUG{$what};
  1416.         } elsif ($what =~ /^\d/) {
  1417.             $CPAN::DEBUG = $what;
  1418.         } elsif (lc $what eq 'all') {
  1419.             my($max) = 0;
  1420.             for (values %CPAN::DEBUG) {
  1421.             $max += $_;
  1422.             }
  1423.             $CPAN::DEBUG = $max;
  1424.         } else {
  1425.             my($known) = 0;
  1426.             for (keys %CPAN::DEBUG) {
  1427.             next unless lc($_) eq lc($what);
  1428.             $CPAN::DEBUG |= $CPAN::DEBUG{$_};
  1429.             $known = 1;
  1430.             }
  1431.             $CPAN::Frontend->myprint("unknown argument [$what]\n")
  1432.             unless $known;
  1433.         }
  1434.         }
  1435.     } else {
  1436.       my $raw = "Valid options for debug are ".
  1437.           join(", ",sort(keys %CPAN::DEBUG), 'all').
  1438.           qq{ or a number. Completion works on the options. }.
  1439.               qq{Case is ignored.};
  1440.       require Text::Wrap;
  1441.       $CPAN::Frontend->myprint(Text::Wrap::fill("","",$raw));
  1442.       $CPAN::Frontend->myprint("\n\n");
  1443.     }
  1444.     if ($CPAN::DEBUG) {
  1445.         $CPAN::Frontend->myprint("Options set for debugging:\n");
  1446.         my($k,$v);
  1447.         for $k (sort {$CPAN::DEBUG{$a} <=> $CPAN::DEBUG{$b}} keys %CPAN::DEBUG) {
  1448.         $v = $CPAN::DEBUG{$k};
  1449.         $CPAN::Frontend->myprint(sprintf "    %-14s(%s)\n", $k, $v)
  1450.                     if $v & $CPAN::DEBUG;
  1451.         }
  1452.     } else {
  1453.         $CPAN::Frontend->myprint("Debugging turned off completely.\n");
  1454.     }
  1455.     } else {
  1456.     $CPAN::Frontend->myprint(qq{
  1457. Known options:
  1458.   conf    set or get configuration variables
  1459.   debug   set or get debugging options
  1460. });
  1461.     }
  1462. }
  1463.  
  1464. sub paintdots_onreload {
  1465.     my($ref) = shift;
  1466.     sub {
  1467.     if ( $_[0] =~ /[Ss]ubroutine (\w+) redefined/ ) {
  1468.         my($subr) = $1;
  1469.         ++$$ref;
  1470.         local($|) = 1;
  1471.         # $CPAN::Frontend->myprint(".($subr)");
  1472.         $CPAN::Frontend->myprint(".");
  1473.         return;
  1474.     }
  1475.     warn @_;
  1476.     };
  1477. }
  1478.  
  1479. #-> sub CPAN::Shell::reload ;
  1480. sub reload {
  1481.     my($self,$command,@arg) = @_;
  1482.     $command ||= "";
  1483.     $self->debug("self[$self]command[$command]arg[@arg]") if $CPAN::DEBUG;
  1484.     if ($command =~ /cpan/i) {
  1485.     CPAN->debug("reloading the whole CPAN.pm") if $CPAN::DEBUG;
  1486.     my $fh = FileHandle->new($INC{'CPAN.pm'});
  1487.     local($/);
  1488.     my $redef = 0;
  1489.     local($SIG{__WARN__}) = paintdots_onreload(\$redef);
  1490.     eval <$fh>;
  1491.     warn $@ if $@;
  1492.     $CPAN::Frontend->myprint("\n$redef subroutines redefined\n");
  1493.     } elsif ($command =~ /index/) {
  1494.       CPAN::Index->force_reload;
  1495.     } else {
  1496.       $CPAN::Frontend->myprint(qq{cpan     re-evals the CPAN.pm file
  1497. index    re-reads the index files\n});
  1498.     }
  1499. }
  1500.  
  1501. #-> sub CPAN::Shell::_binary_extensions ;
  1502. sub _binary_extensions {
  1503.     my($self) = shift @_;
  1504.     my(@result,$module,%seen,%need,$headerdone);
  1505.     for $module ($self->expand('Module','/./')) {
  1506.     my $file  = $module->cpan_file;
  1507.     next if $file eq "N/A";
  1508.     next if $file =~ /^Contact Author/;
  1509.         my $dist = $CPAN::META->instance('CPAN::Distribution',$file);
  1510.     next if $dist->isa_perl;
  1511.     next unless $module->xs_file;
  1512.     local($|) = 1;
  1513.     $CPAN::Frontend->myprint(".");
  1514.     push @result, $module;
  1515.     }
  1516. #    print join " | ", @result;
  1517.     $CPAN::Frontend->myprint("\n");
  1518.     return @result;
  1519. }
  1520.  
  1521. #-> sub CPAN::Shell::recompile ;
  1522. sub recompile {
  1523.     my($self) = shift @_;
  1524.     my($module,@module,$cpan_file,%dist);
  1525.     @module = $self->_binary_extensions();
  1526.     for $module (@module){  # we force now and compile later, so we
  1527.                             # don't do it twice
  1528.     $cpan_file = $module->cpan_file;
  1529.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1530.     $pack->force;
  1531.     $dist{$cpan_file}++;
  1532.     }
  1533.     for $cpan_file (sort keys %dist) {
  1534.     $CPAN::Frontend->myprint("  CPAN: Recompiling $cpan_file\n\n");
  1535.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  1536.     $pack->install;
  1537.     $CPAN::Signal = 0; # it's tempting to reset Signal, so we can
  1538.                            # stop a package from recompiling,
  1539.                            # e.g. IO-1.12 when we have perl5.003_10
  1540.     }
  1541. }
  1542.  
  1543. #-> sub CPAN::Shell::_u_r_common ;
  1544. sub _u_r_common {
  1545.     my($self) = shift @_;
  1546.     my($what) = shift @_;
  1547.     CPAN->debug("self[$self] what[$what] args[@_]") if $CPAN::DEBUG;
  1548.     Carp::croak "Usage: \$obj->_u_r_common(a|r|u)" unless
  1549.           $what && $what =~ /^[aru]$/;
  1550.     my(@args) = @_;
  1551.     @args = '/./' unless @args;
  1552.     my(@result,$module,%seen,%need,$headerdone,
  1553.        $version_undefs,$version_zeroes);
  1554.     $version_undefs = $version_zeroes = 0;
  1555.     my $sprintf = "%s%-25s%s %9s %9s  %s\n";
  1556.     my @expand = $self->expand('Module',@args);
  1557.     my $expand = scalar @expand;
  1558.     if (0) { # Looks like noise to me, was very useful for debugging
  1559.              # for metadata cache
  1560.         $CPAN::Frontend->myprint(sprintf "%d matches in the database\n", $expand);
  1561.     }
  1562.     for $module (@expand) {
  1563.     my $file  = $module->cpan_file;
  1564.     next unless defined $file; # ??
  1565.     my($latest) = $module->cpan_version;
  1566.     my($inst_file) = $module->inst_file;
  1567.     my($have);
  1568.     return if $CPAN::Signal;
  1569.     if ($inst_file){
  1570.         if ($what eq "a") {
  1571.         $have = $module->inst_version;
  1572.         } elsif ($what eq "r") {
  1573.         $have = $module->inst_version;
  1574.         local($^W) = 0;
  1575.         if ($have eq "undef"){
  1576.             $version_undefs++;
  1577.         } elsif ($have == 0){
  1578.             $version_zeroes++;
  1579.         }
  1580.         next unless CPAN::Version->vgt($latest, $have);
  1581. # to be pedantic we should probably say:
  1582. #    && !($have eq "undef" && $latest ne "undef" && $latest gt "");
  1583. # to catch the case where CPAN has a version 0 and we have a version undef
  1584.         } elsif ($what eq "u") {
  1585.         next;
  1586.         }
  1587.     } else {
  1588.         if ($what eq "a") {
  1589.         next;
  1590.         } elsif ($what eq "r") {
  1591.         next;
  1592.         } elsif ($what eq "u") {
  1593.         $have = "-";
  1594.         }
  1595.     }
  1596.     return if $CPAN::Signal; # this is sometimes lengthy
  1597.     $seen{$file} ||= 0;
  1598.     if ($what eq "a") {
  1599.         push @result, sprintf "%s %s\n", $module->id, $have;
  1600.     } elsif ($what eq "r") {
  1601.         push @result, $module->id;
  1602.         next if $seen{$file}++;
  1603.     } elsif ($what eq "u") {
  1604.         push @result, $module->id;
  1605.         next if $seen{$file}++;
  1606.         next if $file =~ /^Contact/;
  1607.     }
  1608.     unless ($headerdone++){
  1609.         $CPAN::Frontend->myprint("\n");
  1610.         $CPAN::Frontend->myprint(sprintf(
  1611.                                              $sprintf,
  1612.                                              "",
  1613.                                              "Package namespace",
  1614.                                              "",
  1615.                                              "installed",
  1616.                                              "latest",
  1617.                                              "in CPAN file"
  1618.                                             ));
  1619.     }
  1620.         my $color_on = "";
  1621.         my $color_off = "";
  1622.         if (
  1623.             $COLOR_REGISTERED
  1624.             &&
  1625.             $CPAN::META->has_inst("Term::ANSIColor")
  1626.             &&
  1627.             $module->{RO}{description}
  1628.            ) {
  1629.             $color_on = Term::ANSIColor::color("green");
  1630.             $color_off = Term::ANSIColor::color("reset");
  1631.         }
  1632.     $CPAN::Frontend->myprint(sprintf $sprintf,
  1633.                                  $color_on,
  1634.                                  $module->id,
  1635.                                  $color_off,
  1636.                                  $have,
  1637.                                  $latest,
  1638.                                  $file);
  1639.     $need{$module->id}++;
  1640.     }
  1641.     unless (%need) {
  1642.     if ($what eq "u") {
  1643.         $CPAN::Frontend->myprint("No modules found for @args\n");
  1644.     } elsif ($what eq "r") {
  1645.         $CPAN::Frontend->myprint("All modules are up to date for @args\n");
  1646.     }
  1647.     }
  1648.     if ($what eq "r") {
  1649.     if ($version_zeroes) {
  1650.         my $s_has = $version_zeroes > 1 ? "s have" : " has";
  1651.         $CPAN::Frontend->myprint(qq{$version_zeroes installed module$s_has }.
  1652.         qq{a version number of 0\n});
  1653.     }
  1654.     if ($version_undefs) {
  1655.         my $s_has = $version_undefs > 1 ? "s have" : " has";
  1656.         $CPAN::Frontend->myprint(qq{$version_undefs installed module$s_has no }.
  1657.         qq{parseable version number\n});
  1658.     }
  1659.     }
  1660.     @result;
  1661. }
  1662.  
  1663. #-> sub CPAN::Shell::r ;
  1664. sub r {
  1665.     shift->_u_r_common("r",@_);
  1666. }
  1667.  
  1668. #-> sub CPAN::Shell::u ;
  1669. sub u {
  1670.     shift->_u_r_common("u",@_);
  1671. }
  1672.  
  1673. #-> sub CPAN::Shell::autobundle ;
  1674. sub autobundle {
  1675.     my($self) = shift;
  1676.     CPAN::Config->load unless $CPAN::Config_loaded++;
  1677.     my(@bundle) = $self->_u_r_common("a",@_);
  1678.     my($todir) = MM->catdir($CPAN::Config->{'cpan_home'},"Bundle");
  1679.     File::Path::mkpath($todir);
  1680.     unless (-d $todir) {
  1681.     $CPAN::Frontend->myprint("Couldn't mkdir $todir for some reason\n");
  1682.     return;
  1683.     }
  1684.     my($y,$m,$d) =  (localtime)[5,4,3];
  1685.     $y+=1900;
  1686.     $m++;
  1687.     my($c) = 0;
  1688.     my($me) = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, $c;
  1689.     my($to) = MM->catfile($todir,"$me.pm");
  1690.     while (-f $to) {
  1691.     $me = sprintf "Snapshot_%04d_%02d_%02d_%02d", $y, $m, $d, ++$c;
  1692.     $to = MM->catfile($todir,"$me.pm");
  1693.     }
  1694.     my($fh) = FileHandle->new(">$to") or Carp::croak "Can't open >$to: $!";
  1695.     $fh->print(
  1696.            "package Bundle::$me;\n\n",
  1697.            "\$VERSION = '0.01';\n\n",
  1698.            "1;\n\n",
  1699.            "__END__\n\n",
  1700.            "=head1 NAME\n\n",
  1701.            "Bundle::$me - Snapshot of installation on ",
  1702.            $Config::Config{'myhostname'},
  1703.            " on ",
  1704.            scalar(localtime),
  1705.            "\n\n=head1 SYNOPSIS\n\n",
  1706.            "perl -MCPAN -e 'install Bundle::$me'\n\n",
  1707.            "=head1 CONTENTS\n\n",
  1708.            join("\n", @bundle),
  1709.            "\n\n=head1 CONFIGURATION\n\n",
  1710.            Config->myconfig,
  1711.            "\n\n=head1 AUTHOR\n\n",
  1712.            "This Bundle has been generated automatically ",
  1713.            "by the autobundle routine in CPAN.pm.\n",
  1714.           );
  1715.     $fh->close;
  1716.     $CPAN::Frontend->myprint("\nWrote bundle file
  1717.     $to\n\n");
  1718. }
  1719.  
  1720. #-> sub CPAN::Shell::expandany ;
  1721. sub expandany {
  1722.     my($self,$s) = @_;
  1723.     CPAN->debug("s[$s]") if $CPAN::DEBUG;
  1724.     if ($s =~ m|/|) { # looks like a file
  1725.         $s = CPAN::Distribution->normalize($s);
  1726.         return $CPAN::META->instance('CPAN::Distribution',$s);
  1727.         # Distributions spring into existence, not expand
  1728.     } elsif ($s =~ m|^Bundle::|) {
  1729.         $self->local_bundles; # scanning so late for bundles seems
  1730.                               # both attractive and crumpy: always
  1731.                               # current state but easy to forget
  1732.                               # somewhere
  1733.         return $self->expand('Bundle',$s);
  1734.     } else {
  1735.         return $self->expand('Module',$s)
  1736.             if $CPAN::META->exists('CPAN::Module',$s);
  1737.     }
  1738.     return;
  1739. }
  1740.  
  1741. #-> sub CPAN::Shell::expand ;
  1742. sub expand {
  1743.     shift;
  1744.     my($type,@args) = @_;
  1745.     my($arg,@m);
  1746.     CPAN->debug("type[$type]args[@args]") if $CPAN::DEBUG;
  1747.     for $arg (@args) {
  1748.     my($regex,$command);
  1749.     if ($arg =~ m|^/(.*)/$|) {
  1750.         $regex = $1;
  1751.     } elsif ($arg =~ m/=/) {
  1752.             $command = 1;
  1753.         }
  1754.     my $class = "CPAN::$type";
  1755.     my $obj;
  1756.         CPAN->debug(sprintf "class[%s]regex[%s]command[%s]",
  1757.                     $class,
  1758.                     defined $regex ? $regex : "UNDEFINED",
  1759.                     $command || "UNDEFINED",
  1760.                    ) if $CPAN::DEBUG;
  1761.     if (defined $regex) {
  1762.             for $obj (
  1763.                       sort
  1764.                       {$a->id cmp $b->id}
  1765.                       $CPAN::META->all_objects($class)
  1766.                      ) {
  1767.                 unless ($obj->id){
  1768.                     # BUG, we got an empty object somewhere
  1769.                     require Data::Dumper;
  1770.                     CPAN->debug(sprintf(
  1771.                                         "Bug in CPAN: Empty id on obj[%s][%s]",
  1772.                                         $obj,
  1773.                                         Data::Dumper::Dumper($obj)
  1774.                                        )) if $CPAN::DEBUG;
  1775.                     next;
  1776.                 }
  1777.                 push @m, $obj
  1778.                     if $obj->id =~ /$regex/i
  1779.                         or
  1780.                             (
  1781.                              (
  1782.                               $] < 5.00303 ### provide sort of
  1783.                               ### compatibility with 5.003
  1784.                               ||
  1785.                               $obj->can('name')
  1786.                              )
  1787.                              &&
  1788.                              $obj->name  =~ /$regex/i
  1789.                             );
  1790.             }
  1791.         } elsif ($command) {
  1792.             die "equal sign in command disabled (immature interface), ".
  1793.                 "you can set
  1794.  ! \$CPAN::Shell::ADVANCED_QUERY=1
  1795. to enable it. But please note, this is HIGHLY EXPERIMENTAL code
  1796. that may go away anytime.\n"
  1797.                     unless $ADVANCED_QUERY;
  1798.             my($method,$criterion) = $arg =~ /(.+?)=(.+)/;
  1799.             my($matchcrit) = $criterion =~ m/^~(.+)/;
  1800.             for my $self (
  1801.                           sort
  1802.                           {$a->id cmp $b->id}
  1803.                           $CPAN::META->all_objects($class)
  1804.                          ) {
  1805.                 my $lhs = $self->$method() or next; # () for 5.00503
  1806.                 if ($matchcrit) {
  1807.                     push @m, $self if $lhs =~ m/$matchcrit/;
  1808.                 } else {
  1809.                     push @m, $self if $lhs eq $criterion;
  1810.                 }
  1811.             }
  1812.     } else {
  1813.         my($xarg) = $arg;
  1814.         if ( $type eq 'Bundle' ) {
  1815.         $xarg =~ s/^(Bundle::)?(.*)/Bundle::$2/;
  1816.         } elsif ($type eq "Distribution") {
  1817.                 $xarg = CPAN::Distribution->normalize($arg);
  1818.             }
  1819.         if ($CPAN::META->exists($class,$xarg)) {
  1820.         $obj = $CPAN::META->instance($class,$xarg);
  1821.         } elsif ($CPAN::META->exists($class,$arg)) {
  1822.         $obj = $CPAN::META->instance($class,$arg);
  1823.         } else {
  1824.         next;
  1825.         }
  1826.         push @m, $obj;
  1827.     }
  1828.     }
  1829.     return wantarray ? @m : $m[0];
  1830. }
  1831.  
  1832. #-> sub CPAN::Shell::format_result ;
  1833. sub format_result {
  1834.     my($self) = shift;
  1835.     my($type,@args) = @_;
  1836.     @args = '/./' unless @args;
  1837.     my(@result) = $self->expand($type,@args);
  1838.     my $result = @result == 1 ?
  1839.     $result[0]->as_string :
  1840.             @result == 0 ?
  1841.                 "No objects of type $type found for argument @args\n" :
  1842.                     join("",
  1843.                          (map {$_->as_glimpse} @result),
  1844.                          scalar @result, " items found\n",
  1845.                         );
  1846.     $result;
  1847. }
  1848.  
  1849. # The only reason for this method is currently to have a reliable
  1850. # debugging utility that reveals which output is going through which
  1851. # channel. No, I don't like the colors ;-)
  1852.  
  1853. #-> sub CPAN::Shell::print_ornameted ;
  1854. sub print_ornamented {
  1855.     my($self,$what,$ornament) = @_;
  1856.     my $longest = 0;
  1857.     return unless defined $what;
  1858.  
  1859.     if ($CPAN::Config->{term_is_latin}){
  1860.         # courtesy jhi:
  1861.         $what
  1862.             =~ s{([\xC0-\xDF])([\x80-\xBF])}{chr(ord($1)<<6&0xC0|ord($2)&0x3F)}eg; #};
  1863.     }
  1864.     if ($PRINT_ORNAMENTING) {
  1865.     unless (defined &color) {
  1866.         if ($CPAN::META->has_inst("Term::ANSIColor")) {
  1867.         import Term::ANSIColor "color";
  1868.         } else {
  1869.         *color = sub { return "" };
  1870.         }
  1871.     }
  1872.     my $line;
  1873.     for $line (split /\n/, $what) {
  1874.         $longest = length($line) if length($line) > $longest;
  1875.     }
  1876.     my $sprintf = "%-" . $longest . "s";
  1877.     while ($what){
  1878.         $what =~ s/(.*\n?)//m;
  1879.         my $line = $1;
  1880.         last unless $line;
  1881.         my($nl) = chomp $line ? "\n" : "";
  1882.         #    print "line[$line]ornament[$ornament]sprintf[$sprintf]\n";
  1883.         print color($ornament), sprintf($sprintf,$line), color("reset"), $nl;
  1884.     }
  1885.     } else {
  1886.     print $what;
  1887.     }
  1888. }
  1889.  
  1890. sub myprint {
  1891.     my($self,$what) = @_;
  1892.  
  1893.     $self->print_ornamented($what, 'bold blue on_yellow');
  1894. }
  1895.  
  1896. sub myexit {
  1897.     my($self,$what) = @_;
  1898.     $self->myprint($what);
  1899.     exit;
  1900. }
  1901.  
  1902. sub mywarn {
  1903.     my($self,$what) = @_;
  1904.     $self->print_ornamented($what, 'bold red on_yellow');
  1905. }
  1906.  
  1907. sub myconfess {
  1908.     my($self,$what) = @_;
  1909.     $self->print_ornamented($what, 'bold red on_white');
  1910.     Carp::confess "died";
  1911. }
  1912.  
  1913. sub mydie {
  1914.     my($self,$what) = @_;
  1915.     $self->print_ornamented($what, 'bold red on_white');
  1916.     die "\n";
  1917. }
  1918.  
  1919. sub setup_output {
  1920.     return if -t STDOUT;
  1921.     my $odef = select STDERR;
  1922.     $| = 1;
  1923.     select STDOUT;
  1924.     $| = 1;
  1925.     select $odef;
  1926. }
  1927.  
  1928. #-> sub CPAN::Shell::rematein ;
  1929. # RE-adme||MA-ke||TE-st||IN-stall
  1930. sub rematein {
  1931.     shift;
  1932.     my($meth,@some) = @_;
  1933.     my $pragma = "";
  1934.     if ($meth eq 'force') {
  1935.     $pragma = $meth;
  1936.     $meth = shift @some;
  1937.     }
  1938.     setup_output();
  1939.     CPAN->debug("pragma[$pragma]meth[$meth] some[@some]") if $CPAN::DEBUG;
  1940.  
  1941.     # Here is the place to set "test_count" on all involved parties to
  1942.     # 0. We then can pass this counter on to the involved
  1943.     # distributions and those can refuse to test if test_count > X. In
  1944.     # the first stab at it we could use a 1 for "X".
  1945.  
  1946.     # But when do I reset the distributions to start with 0 again?
  1947.     # Jost suggested to have a random or cycling interaction ID that
  1948.     # we pass through. But the ID is something that is just left lying
  1949.     # around in addition to the counter, so I'd prefer to set the
  1950.     # counter to 0 now, and repeat at the end of the loop. But what
  1951.     # about dependencies? They appear later and are not reset, they
  1952.     # enter the queue but not its copy. How do they get a sensible
  1953.     # test_count?
  1954.  
  1955.     # construct the queue
  1956.     my($s,@s,@qcopy);
  1957.     foreach $s (@some) {
  1958.     my $obj;
  1959.     if (ref $s) {
  1960.             CPAN->debug("s is an object[$s]") if $CPAN::DEBUG;
  1961.         $obj = $s;
  1962.     } elsif ($s =~ m|^/|) { # looks like a regexp
  1963.             $CPAN::Frontend->mywarn("Sorry, $meth with a regular expression is ".
  1964.                                     "not supported\n");
  1965.             sleep 2;
  1966.             next;
  1967.     } else {
  1968.             CPAN->debug("calling expandany [$s]") if $CPAN::DEBUG;
  1969.         $obj = CPAN::Shell->expandany($s);
  1970.     }
  1971.     if (ref $obj) {
  1972.             $obj->color_cmd_tmps(0,1);
  1973.             CPAN::Queue->new($obj->id);
  1974.             push @qcopy, $obj;
  1975.     } elsif ($CPAN::META->exists('CPAN::Author',$s)) {
  1976.         $obj = $CPAN::META->instance('CPAN::Author',$s);
  1977.             if ($meth eq "dump") {
  1978.                 $obj->dump;
  1979.             } else {
  1980.                 $CPAN::Frontend->myprint(
  1981.                                          join "",
  1982.                                          "Don't be silly, you can't $meth ",
  1983.                                          $obj->fullname,
  1984.                                          " ;-)\n"
  1985.                                         );
  1986.                 sleep 2;
  1987.             }
  1988.     } else {
  1989.         $CPAN::Frontend
  1990.         ->myprint(qq{Warning: Cannot $meth $s, }.
  1991.               qq{don\'t know what it is.
  1992. Try the command
  1993.  
  1994.     i /$s/
  1995.  
  1996. to find objects with matching identifiers.
  1997. });
  1998.             sleep 2;
  1999.     }
  2000.     }
  2001.  
  2002.     # queuerunner (please be warned: when I started to change the
  2003.     # queue to hold objects instead of names, I made one or two
  2004.     # mistakes and never found which. I reverted back instead)
  2005.     while ($s = CPAN::Queue->first) {
  2006.         my $obj;
  2007.     if (ref $s) {
  2008.         $obj = $s; # I do not believe, we would survive if this happened
  2009.     } else {
  2010.         $obj = CPAN::Shell->expandany($s);
  2011.     }
  2012.         if ($pragma
  2013.             &&
  2014.             ($] < 5.00303 || $obj->can($pragma))){
  2015.             ### compatibility with 5.003
  2016.             $obj->$pragma($meth); # the pragma "force" in
  2017.                                   # "CPAN::Distribution" must know
  2018.                                   # what we are intending
  2019.         }
  2020.         if ($]>=5.00303 && $obj->can('called_for')) {
  2021.             $obj->called_for($s);
  2022.         }
  2023.         CPAN->debug(
  2024.                     qq{pragma[$pragma]meth[$meth]obj[$obj]as_string\[}.
  2025.                     $obj->as_string.
  2026.                     qq{\]}
  2027.                    ) if $CPAN::DEBUG;
  2028.  
  2029.         if ($obj->$meth()){
  2030.             CPAN::Queue->delete($s);
  2031.         } else {
  2032.             CPAN->debug("failed");
  2033.         }
  2034.  
  2035.         $obj->undelay;
  2036.     CPAN::Queue->delete_first($s);
  2037.     }
  2038.     for my $obj (@qcopy) {
  2039.         $obj->color_cmd_tmps(0,0);
  2040.     }
  2041. }
  2042.  
  2043. #-> sub CPAN::Shell::dump ;
  2044. sub dump    { shift->rematein('dump',@_); }
  2045. #-> sub CPAN::Shell::force ;
  2046. sub force   { shift->rematein('force',@_); }
  2047. #-> sub CPAN::Shell::get ;
  2048. sub get     { shift->rematein('get',@_); }
  2049. #-> sub CPAN::Shell::readme ;
  2050. sub readme  { shift->rematein('readme',@_); }
  2051. #-> sub CPAN::Shell::make ;
  2052. sub make    { shift->rematein('make',@_); }
  2053. #-> sub CPAN::Shell::test ;
  2054. sub test    { shift->rematein('test',@_); }
  2055. #-> sub CPAN::Shell::install ;
  2056. sub install { shift->rematein('install',@_); }
  2057. #-> sub CPAN::Shell::clean ;
  2058. sub clean   { shift->rematein('clean',@_); }
  2059. #-> sub CPAN::Shell::look ;
  2060. sub look   { shift->rematein('look',@_); }
  2061. #-> sub CPAN::Shell::cvs_import ;
  2062. sub cvs_import   { shift->rematein('cvs_import',@_); }
  2063.  
  2064. package CPAN::LWP::UserAgent;
  2065.  
  2066. sub config {
  2067.     return if $SETUPDONE;
  2068.     if ($CPAN::META->has_usable('LWP::UserAgent')) {
  2069.         require LWP::UserAgent;
  2070.         @ISA = qw(Exporter LWP::UserAgent);
  2071.         $SETUPDONE++;
  2072.     } else {
  2073.         $CPAN::Frontent->mywarn("LWP::UserAgent not available\n");
  2074.     }
  2075. }
  2076.  
  2077. sub get_basic_credentials {
  2078.     my($self, $realm, $uri, $proxy) = @_;
  2079.     return unless $proxy;
  2080.     if ($USER && $PASSWD) {
  2081.     } elsif (defined $CPAN::Config->{proxy_user} &&
  2082.         defined $CPAN::Config->{proxy_pass}) {
  2083.         $USER = $CPAN::Config->{proxy_user};
  2084.         $PASSWD = $CPAN::Config->{proxy_pass};
  2085.     } else {
  2086.         require ExtUtils::MakeMaker;
  2087.         ExtUtils::MakeMaker->import(qw(prompt));
  2088.         $USER = prompt("Proxy authentication needed!
  2089.  (Note: to permanently configure username and password run
  2090.    o conf proxy_user your_username
  2091.    o conf proxy_pass your_password
  2092.  )\nUsername:");
  2093.         if ($CPAN::META->has_inst("Term::ReadKey")) {
  2094.             Term::ReadKey::ReadMode("noecho");
  2095.         } else {
  2096.             $CPAN::Frontend->mywarn("Warning: Term::ReadKey seems not to be available, your password will be echoed to the terminal!\n");
  2097.         }
  2098.         $PASSWD = prompt("Password:");
  2099.         if ($CPAN::META->has_inst("Term::ReadKey")) {
  2100.             Term::ReadKey::ReadMode("restore");
  2101.         }
  2102.         $CPAN::Frontend->myprint("\n\n");
  2103.     }
  2104.     return($USER,$PASSWD);
  2105. }
  2106.  
  2107. sub mirror {
  2108.     my($self,$url,$aslocal) = @_;
  2109.     my $result = $self->SUPER::mirror($url,$aslocal);
  2110.     if ($result->code == 407) {
  2111.         undef $USER;
  2112.         undef $PASSWD;
  2113.         $result = $self->SUPER::mirror($url,$aslocal);
  2114.     }
  2115.     $result;
  2116. }
  2117.  
  2118. package CPAN::FTP;
  2119.  
  2120. #-> sub CPAN::FTP::ftp_get ;
  2121. sub ftp_get {
  2122.   my($class,$host,$dir,$file,$target) = @_;
  2123.   $class->debug(
  2124.         qq[Going to fetch file [$file] from dir [$dir]
  2125.     on host [$host] as local [$target]\n]
  2126.               ) if $CPAN::DEBUG;
  2127.   my $ftp = Net::FTP->new($host);
  2128.   return 0 unless defined $ftp;
  2129.   $ftp->debug(1) if $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG;
  2130.   $class->debug(qq[Going to login("anonymous","$Config::Config{cf_email}")]);
  2131.   unless ( $ftp->login("anonymous",$Config::Config{'cf_email'}) ){
  2132.     warn "Couldn't login on $host";
  2133.     return;
  2134.   }
  2135.   unless ( $ftp->cwd($dir) ){
  2136.     warn "Couldn't cwd $dir";
  2137.     return;
  2138.   }
  2139.   $ftp->binary;
  2140.   $class->debug(qq[Going to ->get("$file","$target")\n]) if $CPAN::DEBUG;
  2141.   unless ( $ftp->get($file,$target) ){
  2142.     warn "Couldn't fetch $file from $host\n";
  2143.     return;
  2144.   }
  2145.   $ftp->quit; # it's ok if this fails
  2146.   return 1;
  2147. }
  2148.  
  2149. # If more accuracy is wanted/needed, Chris Leach sent me this patch...
  2150.  
  2151.  # > *** /install/perl/live/lib/CPAN.pm-    Wed Sep 24 13:08:48 1997
  2152.  # > --- /tmp/cp    Wed Sep 24 13:26:40 1997
  2153.  # > ***************
  2154.  # > *** 1562,1567 ****
  2155.  # > --- 1562,1580 ----
  2156.  # >       return 1 if substr($url,0,4) eq "file";
  2157.  # >       return 1 unless $url =~ m|://([^/]+)|;
  2158.  # >       my $host = $1;
  2159.  # > +     my $proxy = $CPAN::Config->{'http_proxy'} || $ENV{'http_proxy'};
  2160.  # > +     if ($proxy) {
  2161.  # > +         $proxy =~ m|://([^/:]+)|;
  2162.  # > +         $proxy = $1;
  2163.  # > +         my $noproxy = $CPAN::Config->{'no_proxy'} || $ENV{'no_proxy'};
  2164.  # > +         if ($noproxy) {
  2165.  # > +             if ($host !~ /$noproxy$/) {
  2166.  # > +                 $host = $proxy;
  2167.  # > +             }
  2168.  # > +         } else {
  2169.  # > +             $host = $proxy;
  2170.  # > +         }
  2171.  # > +     }
  2172.  # >       require Net::Ping;
  2173.  # >       return 1 unless $Net::Ping::VERSION >= 2;
  2174.  # >       my $p;
  2175.  
  2176.  
  2177. #-> sub CPAN::FTP::localize ;
  2178. sub localize {
  2179.     my($self,$file,$aslocal,$force) = @_;
  2180.     $force ||= 0;
  2181.     Carp::croak "Usage: ->localize(cpan_file,as_local_file[,$force])"
  2182.     unless defined $aslocal;
  2183.     $self->debug("file[$file] aslocal[$aslocal] force[$force]")
  2184.     if $CPAN::DEBUG;
  2185.  
  2186.     if ($^O eq 'MacOS') {
  2187.         # Comment by AK on 2000-09-03: Uniq short filenames would be
  2188.         # available in CHECKSUMS file
  2189.         my($name, $path) = File::Basename::fileparse($aslocal, '');
  2190.         if (length($name) > 31) {
  2191.             $name =~ s/(
  2192.                         \.(
  2193.                            readme(\.(gz|Z))? |
  2194.                            (tar\.)?(gz|Z) |
  2195.                            tgz |
  2196.                            zip |
  2197.                            pm\.(gz|Z)
  2198.                           )
  2199.                        )$//x;
  2200.             my $suf = $1;
  2201.             my $size = 31 - length($suf);
  2202.             while (length($name) > $size) {
  2203.                 chop $name;
  2204.             }
  2205.             $name .= $suf;
  2206.             $aslocal = File::Spec->catfile($path, $name);
  2207.         }
  2208.     }
  2209.  
  2210.     return $aslocal if -f $aslocal && -r _ && !($force & 1);
  2211.     my($restore) = 0;
  2212.     if (-f $aslocal){
  2213.     rename $aslocal, "$aslocal.bak";
  2214.     $restore++;
  2215.     }
  2216.  
  2217.     my($aslocal_dir) = File::Basename::dirname($aslocal);
  2218.     File::Path::mkpath($aslocal_dir);
  2219.     $CPAN::Frontend->mywarn(qq{Warning: You are not allowed to write into }.
  2220.     qq{directory "$aslocal_dir".
  2221.     I\'ll continue, but if you encounter problems, they may be due
  2222.     to insufficient permissions.\n}) unless -w $aslocal_dir;
  2223.  
  2224.     # Inheritance is not easier to manage than a few if/else branches
  2225.     if ($CPAN::META->has_usable('LWP::UserAgent')) {
  2226.      unless ($Ua) {
  2227.             CPAN::LWP::UserAgent->config;
  2228.         eval {$Ua = CPAN::LWP::UserAgent->new;}; # Why is has_usable still not fit enough?
  2229.             if ($@) {
  2230.                 $CPAN::Frontent->mywarn("CPAN::LWP::UserAgent->new dies with $@")
  2231.                     if $CPAN::DEBUG;
  2232.             } else {
  2233.                 my($var);
  2234.                 $Ua->proxy('ftp',  $var)
  2235.                     if $var = $CPAN::Config->{ftp_proxy} || $ENV{ftp_proxy};
  2236.                 $Ua->proxy('http', $var)
  2237.                     if $var = $CPAN::Config->{http_proxy} || $ENV{http_proxy};
  2238.  
  2239.  
  2240. # >>>>> On Wed, 13 Dec 2000 09:21:34 -0500, "Robison, Jonathon (J.M.)" <jrobiso2@visteon.com> said:
  2241. #  > I note that although CPAN.pm can use proxies, it doesn't seem equipped to
  2242. #  > use ones that require basic autorization.
  2243. #  
  2244. #  > Example of when I use it manually in my own stuff:
  2245. #  
  2246. #  > $ua->proxy(['http','ftp'], http://my.proxy.server:83');
  2247. #  > $req->proxy_authorization_basic("username","password");
  2248. #  > $res = $ua->request($req);
  2249.  
  2250.                 $Ua->no_proxy($var)
  2251.                     if $var = $CPAN::Config->{no_proxy} || $ENV{no_proxy};
  2252.             }
  2253.     }
  2254.     }
  2255.     $ENV{ftp_proxy} = $CPAN::Config->{ftp_proxy} if $CPAN::Config->{ftp_proxy};
  2256.     $ENV{http_proxy} = $CPAN::Config->{http_proxy}
  2257.         if $CPAN::Config->{http_proxy};
  2258.     $ENV{no_proxy} = $CPAN::Config->{no_proxy} if $CPAN::Config->{no_proxy};
  2259.  
  2260.     # Try the list of urls for each single object. We keep a record
  2261.     # where we did get a file from
  2262.     my(@reordered,$last);
  2263.     $CPAN::Config->{urllist} ||= [];
  2264.     $last = $#{$CPAN::Config->{urllist}};
  2265.     if ($force & 2) { # local cpans probably out of date, don't reorder
  2266.     @reordered = (0..$last);
  2267.     } else {
  2268.     @reordered =
  2269.         sort {
  2270.         (substr($CPAN::Config->{urllist}[$b],0,4) eq "file")
  2271.             <=>
  2272.         (substr($CPAN::Config->{urllist}[$a],0,4) eq "file")
  2273.             or
  2274.         defined($Thesite)
  2275.             and
  2276.         ($b == $Thesite)
  2277.             <=>
  2278.         ($a == $Thesite)
  2279.         } 0..$last;
  2280.     }
  2281.     my(@levels);
  2282.     if ($Themethod) {
  2283.     @levels = ($Themethod, grep {$_ ne $Themethod} qw/easy hard hardest/);
  2284.     } else {
  2285.     @levels = qw/easy hard hardest/;
  2286.     }
  2287.     @levels = qw/easy/ if $^O eq 'MacOS';
  2288.     my($levelno);
  2289.     for $levelno (0..$#levels) {
  2290.         my $level = $levels[$levelno];
  2291.     my $method = "host$level";
  2292.     my @host_seq = $level eq "easy" ?
  2293.         @reordered : 0..$last;  # reordered has CDROM up front
  2294.     @host_seq = (0) unless @host_seq;
  2295.     my $ret = $self->$method(\@host_seq,$file,$aslocal);
  2296.     if ($ret) {
  2297.       $Themethod = $level;
  2298.       my $now = time;
  2299.       # utime $now, $now, $aslocal; # too bad, if we do that, we
  2300.                                       # might alter a local mirror
  2301.       $self->debug("level[$level]") if $CPAN::DEBUG;
  2302.       return $ret;
  2303.     } else {
  2304.       unlink $aslocal;
  2305.           last if $CPAN::Signal; # need to cleanup
  2306.     }
  2307.     }
  2308.     unless ($CPAN::Signal) {
  2309.         my(@mess);
  2310.         push @mess,
  2311.             qq{Please check, if the URLs I found in your configuration file \(}.
  2312.                 join(", ", @{$CPAN::Config->{urllist}}).
  2313.                     qq{\) are valid. The urllist can be edited.},
  2314.                         qq{E.g. with 'o conf urllist push ftp://myurl/'};
  2315.         $CPAN::Frontend->myprint(Text::Wrap::wrap("","",@mess). "\n\n");
  2316.         sleep 2;
  2317.         $CPAN::Frontend->myprint("Could not fetch $file\n");
  2318.     }
  2319.     if ($restore) {
  2320.     rename "$aslocal.bak", $aslocal;
  2321.     $CPAN::Frontend->myprint("Trying to get away with old file:\n" .
  2322.                  $self->ls($aslocal));
  2323.     return $aslocal;
  2324.     }
  2325.     return;
  2326. }
  2327.  
  2328. sub hosteasy {
  2329.     my($self,$host_seq,$file,$aslocal) = @_;
  2330.     my($i);
  2331.   HOSTEASY: for $i (@$host_seq) {
  2332.         my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2333.     $url .= "/" unless substr($url,-1) eq "/";
  2334.     $url .= $file;
  2335.     $self->debug("localizing perlish[$url]") if $CPAN::DEBUG;
  2336.     if ($url =~ /^file:/) {
  2337.         my $l;
  2338.         if ($CPAN::META->has_inst('URI::URL')) {
  2339.         my $u =  URI::URL->new($url);
  2340.         $l = $u->path;
  2341.         } else { # works only on Unix, is poorly constructed, but
  2342.         # hopefully better than nothing.
  2343.         # RFC 1738 says fileurl BNF is
  2344.         # fileurl = "file://" [ host | "localhost" ] "/" fpath
  2345.         # Thanks to "Mark D. Baushke" <mdb@cisco.com> for
  2346.         # the code
  2347.         ($l = $url) =~ s|^file://[^/]*/|/|; # discard the host part
  2348.         $l =~ s|^file:||;                   # assume they
  2349.                                                     # meant
  2350.                                                     # file://localhost
  2351.         $l =~ s|^/||s unless -f $l;         # e.g. /P:
  2352.         $self->debug("without URI::URL we try local file $l") if $CPAN::DEBUG;
  2353.         }
  2354.         if ( -f $l && -r _) {
  2355.         $Thesite = $i;
  2356.         return $l;
  2357.         }
  2358.         # Maybe mirror has compressed it?
  2359.         if (-f "$l.gz") {
  2360.         $self->debug("found compressed $l.gz") if $CPAN::DEBUG;
  2361.         CPAN::Tarzip->gunzip("$l.gz", $aslocal);
  2362.         if ( -f $aslocal) {
  2363.             $Thesite = $i;
  2364.             return $aslocal;
  2365.         }
  2366.         }
  2367.     }
  2368.         if ($CPAN::META->has_usable('LWP')) {
  2369.       $CPAN::Frontend->myprint("Fetching with LWP:
  2370.   $url
  2371. ");
  2372.       unless ($Ua) {
  2373.               CPAN::LWP::UserAgent->config;
  2374.               eval { $Ua = CPAN::LWP::UserAgent->new; };
  2375.               if ($@) {
  2376.                   $CPAN::Frontent->mywarn("CPAN::LWP::UserAgent->new dies with $@");
  2377.               }
  2378.       }
  2379.       my $res = $Ua->mirror($url, $aslocal);
  2380.       if ($res->is_success) {
  2381.         $Thesite = $i;
  2382.         my $now = time;
  2383.         utime $now, $now, $aslocal; # download time is more
  2384.                                         # important than upload time
  2385.         return $aslocal;
  2386.       } elsif ($url !~ /\.gz(?!\n)\Z/) {
  2387.         my $gzurl = "$url.gz";
  2388.         $CPAN::Frontend->myprint("Fetching with LWP:
  2389.   $gzurl
  2390. ");
  2391.         $res = $Ua->mirror($gzurl, "$aslocal.gz");
  2392.         if ($res->is_success &&
  2393.         CPAN::Tarzip->gunzip("$aslocal.gz",$aslocal)
  2394.            ) {
  2395.           $Thesite = $i;
  2396.           return $aslocal;
  2397.         }
  2398.       } else {
  2399.               $CPAN::Frontend->myprint(sprintf(
  2400.                                                "LWP failed with code[%s] message[%s]\n",
  2401.                                                $res->code,
  2402.                                                $res->message,
  2403.                                               ));
  2404.         # Alan Burlison informed me that in firewall environments
  2405.         # Net::FTP can still succeed where LWP fails. So we do not
  2406.         # skip Net::FTP anymore when LWP is available.
  2407.       }
  2408.     } else {
  2409.             $CPAN::Frontend->myprint("LWP not available\n");
  2410.     }
  2411.         return if $CPAN::Signal;
  2412.     if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2413.         # that's the nice and easy way thanks to Graham
  2414.         my($host,$dir,$getfile) = ($1,$2,$3);
  2415.         if ($CPAN::META->has_usable('Net::FTP')) {
  2416.         $dir =~ s|/+|/|g;
  2417.         $CPAN::Frontend->myprint("Fetching with Net::FTP:
  2418.   $url
  2419. ");
  2420.         $self->debug("getfile[$getfile]dir[$dir]host[$host]" .
  2421.                  "aslocal[$aslocal]") if $CPAN::DEBUG;
  2422.         if (CPAN::FTP->ftp_get($host,$dir,$getfile,$aslocal)) {
  2423.             $Thesite = $i;
  2424.             return $aslocal;
  2425.         }
  2426.         if ($aslocal !~ /\.gz(?!\n)\Z/) {
  2427.             my $gz = "$aslocal.gz";
  2428.             $CPAN::Frontend->myprint("Fetching with Net::FTP
  2429.   $url.gz
  2430. ");
  2431.            if (CPAN::FTP->ftp_get($host,
  2432.                        $dir,
  2433.                        "$getfile.gz",
  2434.                        $gz) &&
  2435.             CPAN::Tarzip->gunzip($gz,$aslocal)
  2436.                ){
  2437.             $Thesite = $i;
  2438.             return $aslocal;
  2439.             }
  2440.         }
  2441.         # next HOSTEASY;
  2442.         }
  2443.     }
  2444.         return if $CPAN::Signal;
  2445.     }
  2446. }
  2447.  
  2448. sub hosthard {
  2449.   my($self,$host_seq,$file,$aslocal) = @_;
  2450.  
  2451.   # Came back if Net::FTP couldn't establish connection (or
  2452.   # failed otherwise) Maybe they are behind a firewall, but they
  2453.   # gave us a socksified (or other) ftp program...
  2454.  
  2455.   my($i);
  2456.   my($devnull) = $CPAN::Config->{devnull} || "";
  2457.   # < /dev/null ";
  2458.   my($aslocal_dir) = File::Basename::dirname($aslocal);
  2459.   File::Path::mkpath($aslocal_dir);
  2460.   HOSTHARD: for $i (@$host_seq) {
  2461.     my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2462.     $url .= "/" unless substr($url,-1) eq "/";
  2463.     $url .= $file;
  2464.     my($proto,$host,$dir,$getfile);
  2465.  
  2466.     # Courtesy Mark Conty mark_conty@cargill.com change from
  2467.     # if ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2468.     # to
  2469.     if ($url =~ m|^([^:]+)://(.*?)/(.*)/(.*)|) {
  2470.       # proto not yet used
  2471.       ($proto,$host,$dir,$getfile) = ($1,$2,$3,$4);
  2472.     } else {
  2473.       next HOSTHARD; # who said, we could ftp anything except ftp?
  2474.     }
  2475.         next HOSTHARD if $proto eq "file"; # file URLs would have had
  2476.                                            # success above. Likely a bogus URL
  2477.  
  2478.     $self->debug("localizing funkyftpwise[$url]") if $CPAN::DEBUG;
  2479.     my($f,$funkyftp);
  2480.     for $f ('lynx','ncftpget','ncftp','wget') {
  2481.       next unless exists $CPAN::Config->{$f};
  2482.       $funkyftp = $CPAN::Config->{$f};
  2483.       next unless defined $funkyftp;
  2484.       next if $funkyftp =~ /^\s*$/;
  2485.       my($asl_ungz, $asl_gz);
  2486.       ($asl_ungz = $aslocal) =~ s/\.gz//;
  2487.           $asl_gz = "$asl_ungz.gz";
  2488.       my($src_switch) = "";
  2489.       if ($f eq "lynx"){
  2490.         $src_switch = " -source";
  2491.       } elsif ($f eq "ncftp"){
  2492.         $src_switch = " -c";
  2493.           } elsif ($f eq "wget"){
  2494.               $src_switch = " -O -";
  2495.       }
  2496.       my($chdir) = "";
  2497.       my($stdout_redir) = " > $asl_ungz";
  2498.       if ($f eq "ncftpget"){
  2499.         $chdir = "cd $aslocal_dir && ";
  2500.         $stdout_redir = "";
  2501.       }
  2502.       $CPAN::Frontend->myprint(
  2503.                    qq[
  2504. Trying with "$funkyftp$src_switch" to get
  2505.     $url
  2506. ]);
  2507.       my($system) =
  2508.           "$chdir$funkyftp$src_switch '$url' $devnull$stdout_redir";
  2509.       $self->debug("system[$system]") if $CPAN::DEBUG;
  2510.       my($wstatus);
  2511.       if (($wstatus = system($system)) == 0
  2512.           &&
  2513.           ($f eq "lynx" ?
  2514.            -s $asl_ungz # lynx returns 0 when it fails somewhere
  2515.            : 1
  2516.           )
  2517.          ) {
  2518.         if (-s $aslocal) {
  2519.           # Looks good
  2520.         } elsif ($asl_ungz ne $aslocal) {
  2521.           # test gzip integrity
  2522.           if (CPAN::Tarzip->gtest($asl_ungz)) {
  2523.                   # e.g. foo.tar is gzipped --> foo.tar.gz
  2524.                   rename $asl_ungz, $aslocal;
  2525.           } else {
  2526.                   CPAN::Tarzip->gzip($asl_ungz,$asl_gz);
  2527.           }
  2528.         }
  2529.         $Thesite = $i;
  2530.         return $aslocal;
  2531.       } elsif ($url !~ /\.gz(?!\n)\Z/) {
  2532.         unlink $asl_ungz if
  2533.         -f $asl_ungz && -s _ == 0;
  2534.         my $gz = "$aslocal.gz";
  2535.         my $gzurl = "$url.gz";
  2536.         $CPAN::Frontend->myprint(
  2537.                      qq[
  2538. Trying with "$funkyftp$src_switch" to get
  2539.   $url.gz
  2540. ]);
  2541.         my($system) = "$funkyftp$src_switch '$url.gz' $devnull > $asl_gz";
  2542.         $self->debug("system[$system]") if $CPAN::DEBUG;
  2543.         my($wstatus);
  2544.         if (($wstatus = system($system)) == 0
  2545.         &&
  2546.         -s $asl_gz
  2547.            ) {
  2548.           # test gzip integrity
  2549.           if (CPAN::Tarzip->gtest($asl_gz)) {
  2550.                   CPAN::Tarzip->gunzip($asl_gz,$aslocal);
  2551.           } else {
  2552.                   # somebody uncompressed file for us?
  2553.                   rename $asl_ungz, $aslocal;
  2554.           }
  2555.           $Thesite = $i;
  2556.           return $aslocal;
  2557.         } else {
  2558.           unlink $asl_gz if -f $asl_gz;
  2559.         }
  2560.       } else {
  2561.         my $estatus = $wstatus >> 8;
  2562.         my $size = -f $aslocal ?
  2563.         ", left\n$aslocal with size ".-s _ :
  2564.             "\nWarning: expected file [$aslocal] doesn't exist";
  2565.         $CPAN::Frontend->myprint(qq{
  2566. System call "$system"
  2567. returned status $estatus (wstat $wstatus)$size
  2568. });
  2569.       }
  2570.           return if $CPAN::Signal;
  2571.     } # lynx,ncftpget,ncftp
  2572.     } # host
  2573. }
  2574.  
  2575. sub hosthardest {
  2576.     my($self,$host_seq,$file,$aslocal) = @_;
  2577.  
  2578.     my($i);
  2579.     my($aslocal_dir) = File::Basename::dirname($aslocal);
  2580.     File::Path::mkpath($aslocal_dir);
  2581.   HOSTHARDEST: for $i (@$host_seq) {
  2582.     unless (length $CPAN::Config->{'ftp'}) {
  2583.         $CPAN::Frontend->myprint("No external ftp command available\n\n");
  2584.         last HOSTHARDEST;
  2585.     }
  2586.     my $url = $CPAN::Config->{urllist}[$i] || $CPAN::Defaultsite;
  2587.     $url .= "/" unless substr($url,-1) eq "/";
  2588.     $url .= $file;
  2589.     $self->debug("localizing ftpwise[$url]") if $CPAN::DEBUG;
  2590.     unless ($url =~ m|^ftp://(.*?)/(.*)/(.*)|) {
  2591.         next;
  2592.     }
  2593.     my($host,$dir,$getfile) = ($1,$2,$3);
  2594.     my $timestamp = 0;
  2595.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
  2596.        $ctime,$blksize,$blocks) = stat($aslocal);
  2597.     $timestamp = $mtime ||= 0;
  2598.     my($netrc) = CPAN::FTP::netrc->new;
  2599.     my($netrcfile) = $netrc->netrc;
  2600.     my($verbose) = $CPAN::DEBUG{'FTP'} & $CPAN::DEBUG ? " -v" : "";
  2601.     my $targetfile = File::Basename::basename($aslocal);
  2602.     my(@dialog);
  2603.     push(
  2604.          @dialog,
  2605.          "lcd $aslocal_dir",
  2606.          "cd /",
  2607.          map("cd $_", split "/", $dir), # RFC 1738
  2608.          "bin",
  2609.          "get $getfile $targetfile",
  2610.          "quit"
  2611.         );
  2612.     if (! $netrcfile) {
  2613.         CPAN->debug("No ~/.netrc file found") if $CPAN::DEBUG;
  2614.     } elsif ($netrc->hasdefault || $netrc->contains($host)) {
  2615.         CPAN->debug(sprintf("hasdef[%d]cont($host)[%d]",
  2616.                 $netrc->hasdefault,
  2617.                 $netrc->contains($host))) if $CPAN::DEBUG;
  2618.         if ($netrc->protected) {
  2619.         $CPAN::Frontend->myprint(qq{
  2620.   Trying with external ftp to get
  2621.     $url
  2622.   As this requires some features that are not thoroughly tested, we\'re
  2623.   not sure, that we get it right....
  2624.  
  2625. }
  2626.              );
  2627.         $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose $host",
  2628.                 @dialog);
  2629.         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2630.          $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2631.         $mtime ||= 0;
  2632.         if ($mtime > $timestamp) {
  2633.             $CPAN::Frontend->myprint("GOT $aslocal\n");
  2634.             $Thesite = $i;
  2635.             return $aslocal;
  2636.         } else {
  2637.             $CPAN::Frontend->myprint("Hmm... Still failed!\n");
  2638.         }
  2639.                 return if $CPAN::Signal;
  2640.         } else {
  2641.         $CPAN::Frontend->mywarn(qq{Your $netrcfile is not }.
  2642.                     qq{correctly protected.\n});
  2643.         }
  2644.     } else {
  2645.         $CPAN::Frontend->mywarn("Your ~/.netrc neither contains $host
  2646.   nor does it have a default entry\n");
  2647.     }
  2648.  
  2649.     # OK, they don't have a valid ~/.netrc. Use 'ftp -n'
  2650.     # then and login manually to host, using e-mail as
  2651.     # password.
  2652.     $CPAN::Frontend->myprint(qq{Issuing "$CPAN::Config->{'ftp'}$verbose -n"\n});
  2653.     unshift(
  2654.         @dialog,
  2655.         "open $host",
  2656.         "user anonymous $Config::Config{'cf_email'}"
  2657.            );
  2658.     $self->talk_ftp("$CPAN::Config->{'ftp'}$verbose -n", @dialog);
  2659.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2660.      $atime,$mtime,$ctime,$blksize,$blocks) = stat($aslocal);
  2661.     $mtime ||= 0;
  2662.     if ($mtime > $timestamp) {
  2663.         $CPAN::Frontend->myprint("GOT $aslocal\n");
  2664.         $Thesite = $i;
  2665.         return $aslocal;
  2666.     } else {
  2667.         $CPAN::Frontend->myprint("Bad luck... Still failed!\n");
  2668.     }
  2669.         return if $CPAN::Signal;
  2670.     $CPAN::Frontend->myprint("Can't access URL $url.\n\n");
  2671.     sleep 2;
  2672.     } # host
  2673. }
  2674.  
  2675. sub talk_ftp {
  2676.     my($self,$command,@dialog) = @_;
  2677.     my $fh = FileHandle->new;
  2678.     $fh->open("|$command") or die "Couldn't open ftp: $!";
  2679.     foreach (@dialog) { $fh->print("$_\n") }
  2680.     $fh->close;        # Wait for process to complete
  2681.     my $wstatus = $?;
  2682.     my $estatus = $wstatus >> 8;
  2683.     $CPAN::Frontend->myprint(qq{
  2684. Subprocess "|$command"
  2685.   returned status $estatus (wstat $wstatus)
  2686. }) if $wstatus;
  2687. }
  2688.  
  2689. # find2perl needs modularization, too, all the following is stolen
  2690. # from there
  2691. # CPAN::FTP::ls
  2692. sub ls {
  2693.     my($self,$name) = @_;
  2694.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$sizemm,
  2695.      $atime,$mtime,$ctime,$blksize,$blocks) = lstat($name);
  2696.  
  2697.     my($perms,%user,%group);
  2698.     my $pname = $name;
  2699.  
  2700.     if ($blocks) {
  2701.     $blocks = int(($blocks + 1) / 2);
  2702.     }
  2703.     else {
  2704.     $blocks = int(($sizemm + 1023) / 1024);
  2705.     }
  2706.  
  2707.     if    (-f _) { $perms = '-'; }
  2708.     elsif (-d _) { $perms = 'd'; }
  2709.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  2710.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  2711.     elsif (-p _) { $perms = 'p'; }
  2712.     elsif (-S _) { $perms = 's'; }
  2713.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  2714.  
  2715.     my(@rwx) = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  2716.     my(@moname) = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  2717.     my $tmpmode = $mode;
  2718.     my $tmp = $rwx[$tmpmode & 7];
  2719.     $tmpmode >>= 3;
  2720.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  2721.     $tmpmode >>= 3;
  2722.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  2723.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  2724.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  2725.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  2726.     $perms .= $tmp;
  2727.  
  2728.     my $user = $user{$uid} || $uid;   # too lazy to implement lookup
  2729.     my $group = $group{$gid} || $gid;
  2730.  
  2731.     my($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  2732.     my($timeyear);
  2733.     my($moname) = $moname[$mon];
  2734.     if (-M _ > 365.25 / 2) {
  2735.     $timeyear = $year + 1900;
  2736.     }
  2737.     else {
  2738.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  2739.     }
  2740.  
  2741.     sprintf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  2742.         $ino,
  2743.          $blocks,
  2744.               $perms,
  2745.                 $nlink,
  2746.                 $user,
  2747.                      $group,
  2748.                       $sizemm,
  2749.                           $moname,
  2750.                          $mday,
  2751.                              $timeyear,
  2752.                              $pname;
  2753. }
  2754.  
  2755. package CPAN::FTP::netrc;
  2756.  
  2757. sub new {
  2758.     my($class) = @_;
  2759.     my $file = MM->catfile($ENV{HOME},".netrc");
  2760.  
  2761.     my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  2762.        $atime,$mtime,$ctime,$blksize,$blocks)
  2763.     = stat($file);
  2764.     $mode ||= 0;
  2765.     my $protected = 0;
  2766.  
  2767.     my($fh,@machines,$hasdefault);
  2768.     $hasdefault = 0;
  2769.     $fh = FileHandle->new or die "Could not create a filehandle";
  2770.  
  2771.     if($fh->open($file)){
  2772.     $protected = ($mode & 077) == 0;
  2773.     local($/) = "";
  2774.       NETRC: while (<$fh>) {
  2775.         my(@tokens) = split " ", $_;
  2776.       TOKEN: while (@tokens) {
  2777.         my($t) = shift @tokens;
  2778.         if ($t eq "default"){
  2779.             $hasdefault++;
  2780.             last NETRC;
  2781.         }
  2782.         last TOKEN if $t eq "macdef";
  2783.         if ($t eq "machine") {
  2784.             push @machines, shift @tokens;
  2785.         }
  2786.         }
  2787.     }
  2788.     } else {
  2789.     $file = $hasdefault = $protected = "";
  2790.     }
  2791.  
  2792.     bless {
  2793.        'mach' => [@machines],
  2794.        'netrc' => $file,
  2795.        'hasdefault' => $hasdefault,
  2796.        'protected' => $protected,
  2797.       }, $class;
  2798. }
  2799.  
  2800. # CPAN::FTP::hasdefault;
  2801. sub hasdefault { shift->{'hasdefault'} }
  2802. sub netrc      { shift->{'netrc'}      }
  2803. sub protected  { shift->{'protected'}  }
  2804. sub contains {
  2805.     my($self,$mach) = @_;
  2806.     for ( @{$self->{'mach'}} ) {
  2807.     return 1 if $_ eq $mach;
  2808.     }
  2809.     return 0;
  2810. }
  2811.  
  2812. package CPAN::Complete;
  2813.  
  2814. sub gnu_cpl {
  2815.     my($text, $line, $start, $end) = @_;
  2816.     my(@perlret) = cpl($text, $line, $start);
  2817.     # find longest common match. Can anybody show me how to peruse
  2818.     # T::R::Gnu to have this done automatically? Seems expensive.
  2819.     return () unless @perlret;
  2820.     my($newtext) = $text;
  2821.     for (my $i = length($text)+1;;$i++) {
  2822.     last unless length($perlret[0]) && length($perlret[0]) >= $i;
  2823.     my $try = substr($perlret[0],0,$i);
  2824.     my @tries = grep {substr($_,0,$i) eq $try} @perlret;
  2825.     # warn "try[$try]tries[@tries]";
  2826.     if (@tries == @perlret) {
  2827.         $newtext = $try;
  2828.     } else {
  2829.         last;
  2830.     }
  2831.     }
  2832.     ($newtext,@perlret);
  2833. }
  2834.  
  2835. #-> sub CPAN::Complete::cpl ;
  2836. sub cpl {
  2837.     my($word,$line,$pos) = @_;
  2838.     $word ||= "";
  2839.     $line ||= "";
  2840.     $pos ||= 0;
  2841.     CPAN->debug("word [$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2842.     $line =~ s/^\s*//;
  2843.     if ($line =~ s/^(force\s*)//) {
  2844.     $pos -= length($1);
  2845.     }
  2846.     my @return;
  2847.     if ($pos == 0) {
  2848.     @return = grep /^$word/, @CPAN::Complete::COMMANDS;
  2849.     } elsif ( $line !~ /^[\!abcdghimorutl]/ ) {
  2850.     @return = ();
  2851.     } elsif ($line =~ /^(a|ls)\s/) {
  2852.     @return = cplx('CPAN::Author',uc($word));
  2853.     } elsif ($line =~ /^b\s/) {
  2854.         CPAN::Shell->local_bundles;
  2855.     @return = cplx('CPAN::Bundle',$word);
  2856.     } elsif ($line =~ /^d\s/) {
  2857.     @return = cplx('CPAN::Distribution',$word);
  2858.     } elsif ($line =~ m/^(
  2859.                           [mru]|make|clean|dump|get|test|install|readme|look|cvs_import
  2860.                          )\s/x ) {
  2861.         if ($word =~ /^Bundle::/) {
  2862.             CPAN::Shell->local_bundles;
  2863.         }
  2864.     @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  2865.     } elsif ($line =~ /^i\s/) {
  2866.     @return = cpl_any($word);
  2867.     } elsif ($line =~ /^reload\s/) {
  2868.     @return = cpl_reload($word,$line,$pos);
  2869.     } elsif ($line =~ /^o\s/) {
  2870.     @return = cpl_option($word,$line,$pos);
  2871.     } elsif ($line =~ m/^\S+\s/ ) {
  2872.         # fallback for future commands and what we have forgotten above
  2873.     @return = (cplx('CPAN::Module',$word),cplx('CPAN::Bundle',$word));
  2874.     } else {
  2875.     @return = ();
  2876.     }
  2877.     return @return;
  2878. }
  2879.  
  2880. #-> sub CPAN::Complete::cplx ;
  2881. sub cplx {
  2882.     my($class, $word) = @_;
  2883.     # I believed for many years that this was sorted, today I
  2884.     # realized, it wasn't sorted anymore. Now (rev 1.301 / v 1.55) I
  2885.     # make it sorted again. Maybe sort was dropped when GNU-readline
  2886.     # support came in? The RCS file is difficult to read on that:-(
  2887.     sort grep /^\Q$word\E/, map { $_->id } $CPAN::META->all_objects($class);
  2888. }
  2889.  
  2890. #-> sub CPAN::Complete::cpl_any ;
  2891. sub cpl_any {
  2892.     my($word) = shift;
  2893.     return (
  2894.         cplx('CPAN::Author',$word),
  2895.         cplx('CPAN::Bundle',$word),
  2896.         cplx('CPAN::Distribution',$word),
  2897.         cplx('CPAN::Module',$word),
  2898.        );
  2899. }
  2900.  
  2901. #-> sub CPAN::Complete::cpl_reload ;
  2902. sub cpl_reload {
  2903.     my($word,$line,$pos) = @_;
  2904.     $word ||= "";
  2905.     my(@words) = split " ", $line;
  2906.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2907.     my(@ok) = qw(cpan index);
  2908.     return @ok if @words == 1;
  2909.     return grep /^\Q$word\E/, @ok if @words == 2 && $word;
  2910. }
  2911.  
  2912. #-> sub CPAN::Complete::cpl_option ;
  2913. sub cpl_option {
  2914.     my($word,$line,$pos) = @_;
  2915.     $word ||= "";
  2916.     my(@words) = split " ", $line;
  2917.     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
  2918.     my(@ok) = qw(conf debug);
  2919.     return @ok if @words == 1;
  2920.     return grep /^\Q$word\E/, @ok if @words == 2 && length($word);
  2921.     if (0) {
  2922.     } elsif ($words[1] eq 'index') {
  2923.     return ();
  2924.     } elsif ($words[1] eq 'conf') {
  2925.     return CPAN::Config::cpl(@_);
  2926.     } elsif ($words[1] eq 'debug') {
  2927.     return sort grep /^\Q$word\E/, sort keys %CPAN::DEBUG, 'all';
  2928.     }
  2929. }
  2930.  
  2931. package CPAN::Index;
  2932.  
  2933. #-> sub CPAN::Index::force_reload ;
  2934. sub force_reload {
  2935.     my($class) = @_;
  2936.     $CPAN::Index::LAST_TIME = 0;
  2937.     $class->reload(1);
  2938. }
  2939.  
  2940. #-> sub CPAN::Index::reload ;
  2941. sub reload {
  2942.     my($cl,$force) = @_;
  2943.     my $time = time;
  2944.  
  2945.     # XXX check if a newer one is available. (We currently read it
  2946.     # from time to time)
  2947.     for ($CPAN::Config->{index_expire}) {
  2948.     $_ = 0.001 unless $_ && $_ > 0.001;
  2949.     }
  2950.     unless (1 || $CPAN::Have_warned->{readmetadatacache}++) {
  2951.         # debug here when CPAN doesn't seem to read the Metadata
  2952.         require Carp;
  2953.         Carp::cluck("META-PROTOCOL[$CPAN::META->{PROTOCOL}]");
  2954.     }
  2955.     unless ($CPAN::META->{PROTOCOL}) {
  2956.         $cl->read_metadata_cache;
  2957.         $CPAN::META->{PROTOCOL} ||= "1.0";
  2958.     }
  2959.     if ( $CPAN::META->{PROTOCOL} < PROTOCOL  ) {
  2960.         # warn "Setting last_time to 0";
  2961.         $LAST_TIME = 0; # No warning necessary
  2962.     }
  2963.     return if $LAST_TIME + $CPAN::Config->{index_expire}*86400 > $time
  2964.     and ! $force;
  2965.     if (0) {
  2966.         # IFF we are developing, it helps to wipe out the memory
  2967.         # between reloads, otherwise it is not what a user expects.
  2968.         undef $CPAN::META; # Neue Gruendlichkeit since v1.52(r1.274)
  2969.         $CPAN::META = CPAN->new;
  2970.     }
  2971.     {
  2972.         my($debug,$t2);
  2973.         local $LAST_TIME = $time;
  2974.         local $CPAN::META->{PROTOCOL} = PROTOCOL;
  2975.  
  2976.         my $needshort = $^O eq "dos";
  2977.  
  2978.         $cl->rd_authindex($cl
  2979.                           ->reload_x(
  2980.                                      "authors/01mailrc.txt.gz",
  2981.                                      $needshort ?
  2982.                                      File::Spec->catfile('authors', '01mailrc.gz') :
  2983.                                      File::Spec->catfile('authors', '01mailrc.txt.gz'),
  2984.                                      $force));
  2985.         $t2 = time;
  2986.         $debug = "timing reading 01[".($t2 - $time)."]";
  2987.         $time = $t2;
  2988.         return if $CPAN::Signal; # this is sometimes lengthy
  2989.         $cl->rd_modpacks($cl
  2990.                          ->reload_x(
  2991.                                     "modules/02packages.details.txt.gz",
  2992.                                     $needshort ?
  2993.                                     File::Spec->catfile('modules', '02packag.gz') :
  2994.                                     File::Spec->catfile('modules', '02packages.details.txt.gz'),
  2995.                                     $force));
  2996.         $t2 = time;
  2997.         $debug .= "02[".($t2 - $time)."]";
  2998.         $time = $t2;
  2999.         return if $CPAN::Signal; # this is sometimes lengthy
  3000.         $cl->rd_modlist($cl
  3001.                         ->reload_x(
  3002.                                    "modules/03modlist.data.gz",
  3003.                                    $needshort ?
  3004.                                    File::Spec->catfile('modules', '03mlist.gz') :
  3005.                                    File::Spec->catfile('modules', '03modlist.data.gz'),
  3006.                                    $force));
  3007.         $cl->write_metadata_cache;
  3008.         $t2 = time;
  3009.         $debug .= "03[".($t2 - $time)."]";
  3010.         $time = $t2;
  3011.         CPAN->debug($debug) if $CPAN::DEBUG;
  3012.     }
  3013.     $LAST_TIME = $time;
  3014.     $CPAN::META->{PROTOCOL} = PROTOCOL;
  3015. }
  3016.  
  3017. #-> sub CPAN::Index::reload_x ;
  3018. sub reload_x {
  3019.     my($cl,$wanted,$localname,$force) = @_;
  3020.     $force |= 2; # means we're dealing with an index here
  3021.     CPAN::Config->load; # we should guarantee loading wherever we rely
  3022.                         # on Config XXX
  3023.     $localname ||= $wanted;
  3024.     my $abs_wanted = MM->catfile($CPAN::Config->{'keep_source_where'},
  3025.                    $localname);
  3026.     if (
  3027.     -f $abs_wanted &&
  3028.     -M $abs_wanted < $CPAN::Config->{'index_expire'} &&
  3029.     !($force & 1)
  3030.        ) {
  3031.     my $s = $CPAN::Config->{'index_expire'} == 1 ? "" : "s";
  3032.     $cl->debug(qq{$abs_wanted younger than $CPAN::Config->{'index_expire'} }.
  3033.            qq{day$s. I\'ll use that.});
  3034.     return $abs_wanted;
  3035.     } else {
  3036.     $force |= 1; # means we're quite serious about it.
  3037.     }
  3038.     return CPAN::FTP->localize($wanted,$abs_wanted,$force);
  3039. }
  3040.  
  3041. #-> sub CPAN::Index::rd_authindex ;
  3042. sub rd_authindex {
  3043.     my($cl, $index_target) = @_;
  3044.     my @lines;
  3045.     return unless defined $index_target;
  3046.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3047.     local(*FH);
  3048.     tie *FH, CPAN::Tarzip, $index_target;
  3049.     local($/) = "\n";
  3050.     push @lines, split /\012/ while <FH>;
  3051.     foreach (@lines) {
  3052.     my($userid,$fullname,$email) =
  3053.         m/alias\s+(\S+)\s+\"([^\"\<]+)\s+\<([^\>]+)\>\"/;
  3054.     next unless $userid && $fullname && $email;
  3055.  
  3056.     # instantiate an author object
  3057.      my $userobj = $CPAN::META->instance('CPAN::Author',$userid);
  3058.     $userobj->set('FULLNAME' => $fullname, 'EMAIL' => $email);
  3059.     return if $CPAN::Signal;
  3060.     }
  3061. }
  3062.  
  3063. sub userid {
  3064.   my($self,$dist) = @_;
  3065.   $dist = $self->{'id'} unless defined $dist;
  3066.   my($ret) = $dist =~ m|(?:\w/\w\w/)?([^/]+)/|;
  3067.   $ret;
  3068. }
  3069.  
  3070. #-> sub CPAN::Index::rd_modpacks ;
  3071. sub rd_modpacks {
  3072.     my($self, $index_target) = @_;
  3073.     my @lines;
  3074.     return unless defined $index_target;
  3075.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3076.     my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  3077.     local($/) = "\n";
  3078.     while ($_ = $fh->READLINE) {
  3079.     s/\012/\n/g;
  3080.     my @ls = map {"$_\n"} split /\n/, $_;
  3081.     unshift @ls, "\n" x length($1) if /^(\n+)/;
  3082.     push @lines, @ls;
  3083.     }
  3084.     # read header
  3085.     my($line_count,$last_updated);
  3086.     while (@lines) {
  3087.     my $shift = shift(@lines);
  3088.     last if $shift =~ /^\s*$/;
  3089.     $shift =~ /^Line-Count:\s+(\d+)/ and $line_count = $1;
  3090.         $shift =~ /^Last-Updated:\s+(.+)/ and $last_updated = $1;
  3091.     }
  3092.     if (not defined $line_count) {
  3093.  
  3094.     warn qq{Warning: Your $index_target does not contain a Line-Count header.
  3095. Please check the validity of the index file by comparing it to more
  3096. than one CPAN mirror. I'll continue but problems seem likely to
  3097. happen.\a
  3098. };
  3099.  
  3100.     sleep 5;
  3101.     } elsif ($line_count != scalar @lines) {
  3102.  
  3103.     warn sprintf qq{Warning: Your %s
  3104. contains a Line-Count header of %d but I see %d lines there. Please
  3105. check the validity of the index file by comparing it to more than one
  3106. CPAN mirror. I'll continue but problems seem likely to happen.\a\n},
  3107. $index_target, $line_count, scalar(@lines);
  3108.  
  3109.     }
  3110.     if (not defined $last_updated) {
  3111.  
  3112.     warn qq{Warning: Your $index_target does not contain a Last-Updated header.
  3113. Please check the validity of the index file by comparing it to more
  3114. than one CPAN mirror. I'll continue but problems seem likely to
  3115. happen.\a
  3116. };
  3117.  
  3118.     sleep 5;
  3119.     } else {
  3120.  
  3121.     $CPAN::Frontend
  3122.             ->myprint(sprintf qq{  Database was generated on %s\n},
  3123.                       $last_updated);
  3124.         $DATE_OF_02 = $last_updated;
  3125.  
  3126.         if ($CPAN::META->has_inst(HTTP::Date)) {
  3127.             require HTTP::Date;
  3128.             my($age) = (time - HTTP::Date::str2time($last_updated))/3600/24;
  3129.             if ($age > 30) {
  3130.  
  3131.                 $CPAN::Frontend
  3132.                     ->mywarn(sprintf
  3133.                              qq{Warning: This index file is %d days old.
  3134.   Please check the host you chose as your CPAN mirror for staleness.
  3135.   I'll continue but problems seem likely to happen.\a\n},
  3136.                              $age);
  3137.  
  3138.             }
  3139.         } else {
  3140.             $CPAN::Frontend->myprint("  HTTP::Date not available\n");
  3141.         }
  3142.     }
  3143.  
  3144.  
  3145.     # A necessity since we have metadata_cache: delete what isn't
  3146.     # there anymore
  3147.     my $secondtime = $CPAN::META->exists("CPAN::Module","CPAN");
  3148.     CPAN->debug("secondtime[$secondtime]") if $CPAN::DEBUG;
  3149.     my(%exists);
  3150.     foreach (@lines) {
  3151.     chomp;
  3152.         # before 1.56 we split into 3 and discarded the rest. From
  3153.         # 1.57 we assign remaining text to $comment thus allowing to
  3154.         # influence isa_perl
  3155.     my($mod,$version,$dist,$comment) = split " ", $_, 4;
  3156.     my($bundle,$id,$userid);
  3157.  
  3158.     if ($mod eq 'CPAN' &&
  3159.         ! (
  3160.            CPAN::Queue->exists('Bundle::CPAN') ||
  3161.            CPAN::Queue->exists('CPAN')
  3162.           )
  3163.        ) {
  3164.             local($^W)= 0;
  3165.             if ($version > $CPAN::VERSION){
  3166.                 $CPAN::Frontend->myprint(qq{
  3167.   There's a new CPAN.pm version (v$version) available!
  3168.   [Current version is v$CPAN::VERSION]
  3169.   You might want to try
  3170.     install Bundle::CPAN
  3171.     reload cpan
  3172.   without quitting the current session. It should be a seamless upgrade
  3173.   while we are running...
  3174. }); #});
  3175.                 sleep 2;
  3176.         $CPAN::Frontend->myprint(qq{\n});
  3177.         }
  3178.         last if $CPAN::Signal;
  3179.     } elsif ($mod =~ /^Bundle::(.*)/) {
  3180.         $bundle = $1;
  3181.     }
  3182.  
  3183.     if ($bundle){
  3184.         $id =  $CPAN::META->instance('CPAN::Bundle',$mod);
  3185.         # Let's make it a module too, because bundles have so much
  3186.         # in common with modules.
  3187.  
  3188.             # Changed in 1.57_63: seems like memory bloat now without
  3189.             # any value, so commented out
  3190.  
  3191.         # $CPAN::META->instance('CPAN::Module',$mod);
  3192.  
  3193.     } else {
  3194.  
  3195.         # instantiate a module object
  3196.         $id = $CPAN::META->instance('CPAN::Module',$mod);
  3197.  
  3198.     }
  3199.  
  3200.     if ($id->cpan_file ne $dist){ # update only if file is
  3201.                                       # different. CPAN prohibits same
  3202.                                       # name with different version
  3203.         $userid = $self->userid($dist);
  3204.         $id->set(
  3205.              'CPAN_USERID' => $userid,
  3206.              'CPAN_VERSION' => $version,
  3207.              'CPAN_FILE' => $dist,
  3208.             );
  3209.     }
  3210.  
  3211.     # instantiate a distribution object
  3212.     if ($CPAN::META->exists('CPAN::Distribution',$dist)) {
  3213.       # we do not need CONTAINSMODS unless we do something with
  3214.       # this dist, so we better produce it on demand.
  3215.  
  3216.       ## my $obj = $CPAN::META->instance(
  3217.       ##                   'CPAN::Distribution' => $dist
  3218.       ##                  );
  3219.       ## $obj->{CONTAINSMODS}{$mod} = undef; # experimental
  3220.     } else {
  3221.       $CPAN::META->instance(
  3222.                 'CPAN::Distribution' => $dist
  3223.                    )->set(
  3224.                       'CPAN_USERID' => $userid,
  3225.                                       'CPAN_COMMENT' => $comment,
  3226.                      );
  3227.     }
  3228.         if ($secondtime) {
  3229.             for my $name ($mod,$dist) {
  3230.                 CPAN->debug("exists name[$name]") if $CPAN::DEBUG;
  3231.                 $exists{$name} = undef;
  3232.             }
  3233.         }
  3234.     return if $CPAN::Signal;
  3235.     }
  3236.     undef $fh;
  3237.     if ($secondtime) {
  3238.         for my $class (qw(CPAN::Module CPAN::Bundle CPAN::Distribution)) {
  3239.             for my $o ($CPAN::META->all_objects($class)) {
  3240.                 next if exists $exists{$o->{ID}};
  3241.                 $CPAN::META->delete($class,$o->{ID});
  3242.                 CPAN->debug("deleting ID[$o->{ID}] in class[$class]")
  3243.                     if $CPAN::DEBUG;
  3244.             }
  3245.         }
  3246.     }
  3247. }
  3248.  
  3249. #-> sub CPAN::Index::rd_modlist ;
  3250. sub rd_modlist {
  3251.     my($cl,$index_target) = @_;
  3252.     return unless defined $index_target;
  3253.     $CPAN::Frontend->myprint("Going to read $index_target\n");
  3254.     my $fh = CPAN::Tarzip->TIEHANDLE($index_target);
  3255.     my @eval;
  3256.     local($/) = "\n";
  3257.     while ($_ = $fh->READLINE) {
  3258.     s/\012/\n/g;
  3259.     my @ls = map {"$_\n"} split /\n/, $_;
  3260.     unshift @ls, "\n" x length($1) if /^(\n+)/;
  3261.     push @eval, @ls;
  3262.     }
  3263.     while (@eval) {
  3264.     my $shift = shift(@eval);
  3265.     if ($shift =~ /^Date:\s+(.*)/){
  3266.         return if $DATE_OF_03 eq $1;
  3267.         ($DATE_OF_03) = $1;
  3268.     }
  3269.     last if $shift =~ /^\s*$/;
  3270.     }
  3271.     undef $fh;
  3272.     push @eval, q{CPAN::Modulelist->data;};
  3273.     local($^W) = 0;
  3274.     my($comp) = Safe->new("CPAN::Safe1");
  3275.     my($eval) = join("", @eval);
  3276.     my $ret = $comp->reval($eval);
  3277.     Carp::confess($@) if $@;
  3278.     return if $CPAN::Signal;
  3279.     for (keys %$ret) {
  3280.     my $obj = $CPAN::META->instance("CPAN::Module",$_);
  3281.         delete $ret->{$_}{modid}; # not needed here, maybe elsewhere
  3282.     $obj->set(%{$ret->{$_}});
  3283.     return if $CPAN::Signal;
  3284.     }
  3285. }
  3286.  
  3287. #-> sub CPAN::Index::write_metadata_cache ;
  3288. sub write_metadata_cache {
  3289.     my($self) = @_;
  3290.     return unless $CPAN::Config->{'cache_metadata'};
  3291.     return unless $CPAN::META->has_usable("Storable");
  3292.     my $cache;
  3293.     foreach my $k (qw(CPAN::Bundle CPAN::Author CPAN::Module
  3294.               CPAN::Distribution)) {
  3295.     $cache->{$k} = $CPAN::META->{readonly}{$k}; # unsafe meta access, ok
  3296.     }
  3297.     my $metadata_file = MM->catfile($CPAN::Config->{cpan_home},"Metadata");
  3298.     $cache->{last_time} = $LAST_TIME;
  3299.     $cache->{DATE_OF_02} = $DATE_OF_02;
  3300.     $cache->{PROTOCOL} = PROTOCOL;
  3301.     $CPAN::Frontend->myprint("Going to write $metadata_file\n");
  3302.     eval { Storable::nstore($cache, $metadata_file) };
  3303.     $CPAN::Frontend->mywarn($@) if $@;
  3304. }
  3305.  
  3306. #-> sub CPAN::Index::read_metadata_cache ;
  3307. sub read_metadata_cache {
  3308.     my($self) = @_;
  3309.     return unless $CPAN::Config->{'cache_metadata'};
  3310.     return unless $CPAN::META->has_usable("Storable");
  3311.     my $metadata_file = MM->catfile($CPAN::Config->{cpan_home},"Metadata");
  3312.     return unless -r $metadata_file and -f $metadata_file;
  3313.     $CPAN::Frontend->myprint("Going to read $metadata_file\n");
  3314.     my $cache;
  3315.     eval { $cache = Storable::retrieve($metadata_file) };
  3316.     $CPAN::Frontend->mywarn($@) if $@;
  3317.     if (!$cache || ref $cache ne 'HASH'){
  3318.         $LAST_TIME = 0;
  3319.         return;
  3320.     }
  3321.     if (exists $cache->{PROTOCOL}) {
  3322.         if (PROTOCOL > $cache->{PROTOCOL}) {
  3323.             $CPAN::Frontend->mywarn(sprintf("Ignoring Metadata cache written ".
  3324.                                             "with protocol v%s, requiring v%s",
  3325.                                             $cache->{PROTOCOL},
  3326.                                             PROTOCOL)
  3327.                                    );
  3328.             return;
  3329.         }
  3330.     } else {
  3331.         $CPAN::Frontend->mywarn("Ignoring Metadata cache written ".
  3332.                                 "with protocol v1.0");
  3333.         return;
  3334.     }
  3335.     my $clcnt = 0;
  3336.     my $idcnt = 0;
  3337.     while(my($class,$v) = each %$cache) {
  3338.     next unless $class =~ /^CPAN::/;
  3339.     $CPAN::META->{readonly}{$class} = $v; # unsafe meta access, ok
  3340.         while (my($id,$ro) = each %$v) {
  3341.             $CPAN::META->{readwrite}{$class}{$id} ||=
  3342.                 $class->new(ID=>$id, RO=>$ro);
  3343.             $idcnt++;
  3344.         }
  3345.         $clcnt++;
  3346.     }
  3347.     unless ($clcnt) { # sanity check
  3348.         $CPAN::Frontend->myprint("Warning: Found no data in $metadata_file\n");
  3349.         return;
  3350.     }
  3351.     if ($idcnt < 1000) {
  3352.         $CPAN::Frontend->myprint("Warning: Found only $idcnt objects ".
  3353.                                  "in $metadata_file\n");
  3354.         return;
  3355.     }
  3356.     $CPAN::META->{PROTOCOL} ||=
  3357.         $cache->{PROTOCOL}; # reading does not up or downgrade, but it
  3358.                             # does initialize to some protocol
  3359.     $LAST_TIME = $cache->{last_time};
  3360.     $DATE_OF_02 = $cache->{DATE_OF_02};
  3361.     $CPAN::Frontend->myprint("  Database was generated on $DATE_OF_02\n");
  3362.     return;
  3363. }
  3364.  
  3365. package CPAN::InfoObj;
  3366.  
  3367. # Accessors
  3368. sub cpan_userid { shift->{RO}{CPAN_USERID} }
  3369. sub id { shift->{ID}; }
  3370.  
  3371. #-> sub CPAN::InfoObj::new ;
  3372. sub new {
  3373.     my $this = bless {}, shift;
  3374.     %$this = @_;
  3375.     $this
  3376. }
  3377.  
  3378. # The set method may only be used by code that reads index data or
  3379. # otherwise "objective" data from the outside world. All session
  3380. # related material may do anything else with instance variables but
  3381. # must not touch the hash under the RO attribute. The reason is that
  3382. # the RO hash gets written to Metadata file and is thus persistent.
  3383.  
  3384. #-> sub CPAN::InfoObj::set ;
  3385. sub set {
  3386.     my($self,%att) = @_;
  3387.     my $class = ref $self;
  3388.  
  3389.     # This must be ||=, not ||, because only if we write an empty
  3390.     # reference, only then the set method will write into the readonly
  3391.     # area. But for Distributions that spring into existence, maybe
  3392.     # because of a typo, we do not like it that they are written into
  3393.     # the readonly area and made permanent (at least for a while) and
  3394.     # that is why we do not "allow" other places to call ->set.
  3395.     unless ($self->id) {
  3396.         CPAN->debug("Bug? Empty ID, rejecting");
  3397.         return;
  3398.     }
  3399.     my $ro = $self->{RO} =
  3400.         $CPAN::META->{readonly}{$class}{$self->id} ||= {};
  3401.  
  3402.     while (my($k,$v) = each %att) {
  3403.         $ro->{$k} = $v;
  3404.     }
  3405. }
  3406.  
  3407. #-> sub CPAN::InfoObj::as_glimpse ;
  3408. sub as_glimpse {
  3409.     my($self) = @_;
  3410.     my(@m);
  3411.     my $class = ref($self);
  3412.     $class =~ s/^CPAN:://;
  3413.     push @m, sprintf "%-15s %s\n", $class, $self->{ID};
  3414.     join "", @m;
  3415. }
  3416.  
  3417. #-> sub CPAN::InfoObj::as_string ;
  3418. sub as_string {
  3419.     my($self) = @_;
  3420.     my(@m);
  3421.     my $class = ref($self);
  3422.     $class =~ s/^CPAN:://;
  3423.     push @m, $class, " id = $self->{ID}\n";
  3424.     for (sort keys %{$self->{RO}}) {
  3425.     # next if m/^(ID|RO)$/;
  3426.     my $extra = "";
  3427.     if ($_ eq "CPAN_USERID") {
  3428.             $extra .= " (".$self->author;
  3429.             my $email; # old perls!
  3430.             if ($email = $CPAN::META->instance("CPAN::Author",
  3431.                                                $self->cpan_userid
  3432.                                               )->email) {
  3433.                 $extra .= " <$email>";
  3434.             } else {
  3435.                 $extra .= " <no email>";
  3436.             }
  3437.             $extra .= ")";
  3438.         } elsif ($_ eq "FULLNAME") { # potential UTF-8 conversion
  3439.             push @m, sprintf "    %-12s %s\n", $_, $self->fullname;
  3440.             next;
  3441.         }
  3442.         next unless defined $self->{RO}{$_};
  3443.         push @m, sprintf "    %-12s %s%s\n", $_, $self->{RO}{$_}, $extra;
  3444.     }
  3445.     for (sort keys %$self) {
  3446.     next if m/^(ID|RO)$/;
  3447.     if (ref($self->{$_}) eq "ARRAY") {
  3448.       push @m, sprintf "    %-12s %s\n", $_, "@{$self->{$_}}";
  3449.     } elsif (ref($self->{$_}) eq "HASH") {
  3450.       push @m, sprintf(
  3451.                "    %-12s %s\n",
  3452.                $_,
  3453.                join(" ",keys %{$self->{$_}}),
  3454.                           );
  3455.     } else {
  3456.       push @m, sprintf "    %-12s %s\n", $_, $self->{$_};
  3457.     }
  3458.     }
  3459.     join "", @m, "\n";
  3460. }
  3461.  
  3462. #-> sub CPAN::InfoObj::author ;
  3463. sub author {
  3464.     my($self) = @_;
  3465.     $CPAN::META->instance("CPAN::Author",$self->cpan_userid)->fullname;
  3466. }
  3467.  
  3468. #-> sub CPAN::InfoObj::dump ;
  3469. sub dump {
  3470.   my($self) = @_;
  3471.   require Data::Dumper;
  3472.   print Data::Dumper::Dumper($self);
  3473. }
  3474.  
  3475. package CPAN::Author;
  3476.  
  3477. #-> sub CPAN::Author::id
  3478. sub id {
  3479.     my $self = shift;
  3480.     my $id = $self->{ID};
  3481.     $CPAN::Frontend->mydie("Illegal author id[$id]") unless $id =~ /^[A-Z]/;
  3482.     $id;
  3483. }
  3484.  
  3485. #-> sub CPAN::Author::as_glimpse ;
  3486. sub as_glimpse {
  3487.     my($self) = @_;
  3488.     my(@m);
  3489.     my $class = ref($self);
  3490.     $class =~ s/^CPAN:://;
  3491.     push @m, sprintf(qq{%-15s %s ("%s" <%s>)\n},
  3492.                      $class,
  3493.                      $self->{ID},
  3494.                      $self->fullname,
  3495.                      $self->email);
  3496.     join "", @m;
  3497. }
  3498.  
  3499. #-> sub CPAN::Author::fullname ;
  3500. sub fullname {
  3501.     shift->{RO}{FULLNAME};
  3502. }
  3503. *name = \&fullname;
  3504.  
  3505. #-> sub CPAN::Author::email ;
  3506. sub email    { shift->{RO}{EMAIL}; }
  3507.  
  3508. #-> sub CPAN::Author::ls ;
  3509. sub ls {
  3510.     my $self = shift;
  3511.     my $id = $self->id;
  3512.  
  3513.     # adapted from CPAN::Distribution::verifyMD5 ;
  3514.     my(@csf); # chksumfile
  3515.     @csf = $self->id =~ /(.)(.)(.*)/;
  3516.     $csf[1] = join "", @csf[0,1];
  3517.     $csf[2] = join "", @csf[1,2];
  3518.     my(@dl);
  3519.     @dl = $self->dir_listing([$csf[0],"CHECKSUMS"], 0);
  3520.     unless (grep {$_->[2] eq $csf[1]} @dl) {
  3521.         $CPAN::Frontend->myprint("No files in the directory of $id\n");
  3522.         return;
  3523.     }
  3524.     @dl = $self->dir_listing([@csf[0,1],"CHECKSUMS"], 0);
  3525.     unless (grep {$_->[2] eq $csf[2]} @dl) {
  3526.         $CPAN::Frontend->myprint("No files in the directory of $id\n");
  3527.         return;
  3528.     }
  3529.     @dl = $self->dir_listing([@csf,"CHECKSUMS"], 1);
  3530.     $CPAN::Frontend->myprint(join "", map {
  3531.         sprintf("%8d %10s %s/%s\n", $_->[0], $_->[1], $id, $_->[2])
  3532.     } sort { $a->[2] cmp $b->[2] } @dl);
  3533. }
  3534.  
  3535. # returns an array of arrays, the latter contain (size,mtime,filename)
  3536. #-> sub CPAN::Author::dir_listing ;
  3537. sub dir_listing {
  3538.     my $self = shift;
  3539.     my $chksumfile = shift;
  3540.     my $recursive = shift;
  3541.     my $lc_want =
  3542.     MM->catfile($CPAN::Config->{keep_source_where},
  3543.                     "authors", "id", @$chksumfile);
  3544.     local($") = "/";
  3545.     # connect "force" argument with "index_expire".
  3546.     my $force = 0;
  3547.     if (my @stat = stat $lc_want) {
  3548.         $force = $stat[9] + $CPAN::Config->{index_expire}*86400 <= time;
  3549.     }
  3550.     my $lc_file = CPAN::FTP->localize("authors/id/@$chksumfile",
  3551.                                       $lc_want,$force);
  3552.     unless ($lc_file) {
  3553.         $CPAN::Frontend->myprint("Trying $lc_want.gz\n");
  3554.     $chksumfile->[-1] .= ".gz";
  3555.     $lc_file = CPAN::FTP->localize("authors/id/@$chksumfile",
  3556.                                        "$lc_want.gz",1);
  3557.     if ($lc_file) {
  3558.         $lc_file =~ s{\.gz(?!\n)\Z}{}; #};
  3559.         CPAN::Tarzip->gunzip("$lc_file.gz",$lc_file);
  3560.     } else {
  3561.         return;
  3562.     }
  3563.     }
  3564.  
  3565.     # adapted from CPAN::Distribution::MD5_check_file ;
  3566.     my $fh = FileHandle->new;
  3567.     my($cksum);
  3568.     if (open $fh, $lc_file){
  3569.     local($/);
  3570.     my $eval = <$fh>;
  3571.     $eval =~ s/\015?\012/\n/g;
  3572.     close $fh;
  3573.     my($comp) = Safe->new();
  3574.     $cksum = $comp->reval($eval);
  3575.     if ($@) {
  3576.         rename $lc_file, "$lc_file.bad";
  3577.         Carp::confess($@) if $@;
  3578.     }
  3579.     } else {
  3580.     Carp::carp "Could not open $lc_file for reading";
  3581.     }
  3582.     my(@result,$f);
  3583.     for $f (sort keys %$cksum) {
  3584.         if (exists $cksum->{$f}{isdir}) {
  3585.             if ($recursive) {
  3586.                 my(@dir) = @$chksumfile;
  3587.                 pop @dir;
  3588.                 push @dir, $f, "CHECKSUMS";
  3589.                 push @result, map {
  3590.                     [$_->[0], $_->[1], "$f/$_->[2]"]
  3591.                 } $self->dir_listing(\@dir,1);
  3592.             } else {
  3593.                 push @result, [ 0, "-", $f ];
  3594.             }
  3595.         } else {
  3596.             push @result, [
  3597.                            ($cksum->{$f}{"size"}||0),
  3598.                            $cksum->{$f}{"mtime"}||"---",
  3599.                            $f
  3600.                           ];
  3601.         }
  3602.     }
  3603.     @result;
  3604. }
  3605.  
  3606. package CPAN::Distribution;
  3607.  
  3608. # Accessors
  3609. sub cpan_comment { shift->{RO}{CPAN_COMMENT} }
  3610.  
  3611. sub undelay {
  3612.     my $self = shift;
  3613.     delete $self->{later};
  3614. }
  3615.  
  3616. # CPAN::Distribution::normalize
  3617. sub normalize {
  3618.     my($self,$s) = @_;
  3619.     $s = $self->id unless defined $s;
  3620.     if (
  3621.         $s =~ tr|/|| == 1
  3622.         or
  3623.         $s !~ m|[A-Z]/[A-Z-]{2}/[A-Z-]{2,}/|
  3624.        ) {
  3625.         return $s if $s =~ m:^N/A|^Contact Author: ;
  3626.         $s =~ s|^(.)(.)([^/]*/)(.+)$|$1/$1$2/$1$2$3$4| or
  3627.             $CPAN::Frontend->mywarn("Strange distribution name [$s]");
  3628.         CPAN->debug("s[$s]") if $CPAN::DEBUG;
  3629.     }
  3630.     $s;
  3631. }
  3632.  
  3633. #-> sub CPAN::Distribution::color_cmd_tmps ;
  3634. sub color_cmd_tmps {
  3635.     my($self) = shift;
  3636.     my($depth) = shift || 0;
  3637.     my($color) = shift || 0;
  3638.     # a distribution needs to recurse into its prereq_pms
  3639.  
  3640.     return if exists $self->{incommandcolor}
  3641.         && $self->{incommandcolor}==$color;
  3642.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  3643.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  3644.                                    $depth,
  3645.                                    $self,
  3646.                                    $self->id
  3647.                                   )) if $depth>=100;
  3648.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  3649.     my $prereq_pm = $self->prereq_pm;
  3650.     if (defined $prereq_pm) {
  3651.         for my $pre (keys %$prereq_pm) {
  3652.             my $premo = CPAN::Shell->expand("Module",$pre);
  3653.             $premo->color_cmd_tmps($depth+1,$color);
  3654.         }
  3655.     }
  3656.     if ($color==0) {
  3657.         delete $self->{sponsored_mods};
  3658.         delete $self->{badtestcnt};
  3659.     }
  3660.     $self->{incommandcolor} = $color;
  3661. }
  3662.  
  3663. #-> sub CPAN::Distribution::as_string ;
  3664. sub as_string {
  3665.   my $self = shift;
  3666.   $self->containsmods;
  3667.   $self->SUPER::as_string(@_);
  3668. }
  3669.  
  3670. #-> sub CPAN::Distribution::containsmods ;
  3671. sub containsmods {
  3672.   my $self = shift;
  3673.   return keys %{$self->{CONTAINSMODS}} if exists $self->{CONTAINSMODS};
  3674.   my $dist_id = $self->{ID};
  3675.   for my $mod ($CPAN::META->all_objects("CPAN::Module")) {
  3676.     my $mod_file = $mod->cpan_file or next;
  3677.     my $mod_id = $mod->{ID} or next;
  3678.     # warn "mod_file[$mod_file] dist_id[$dist_id] mod_id[$mod_id]";
  3679.     # sleep 1;
  3680.     $self->{CONTAINSMODS}{$mod_id} = undef if $mod_file eq $dist_id;
  3681.   }
  3682.   keys %{$self->{CONTAINSMODS}};
  3683. }
  3684.  
  3685. #-> sub CPAN::Distribution::uptodate ;
  3686. sub uptodate {
  3687.     my($self) = @_;
  3688.     my $c;
  3689.     foreach $c ($self->containsmods) {
  3690.         my $obj = CPAN::Shell->expandany($c);
  3691.         return 0 unless $obj->uptodate;
  3692.     }
  3693.     return 1;
  3694. }
  3695.  
  3696. #-> sub CPAN::Distribution::called_for ;
  3697. sub called_for {
  3698.     my($self,$id) = @_;
  3699.     $self->{CALLED_FOR} = $id if defined $id;
  3700.     return $self->{CALLED_FOR};
  3701. }
  3702.  
  3703. #-> sub CPAN::Distribution::safe_chdir ;
  3704. sub safe_chdir {
  3705.     my($self,$todir) = @_;
  3706.     # we die if we cannot chdir and we are debuggable
  3707.     Carp::confess("safe_chdir called without todir argument")
  3708.           unless defined $todir and length $todir;
  3709.     if (chdir $todir) {
  3710.         $self->debug(sprintf "changed directory to %s", CPAN::anycwd())
  3711.             if $CPAN::DEBUG;
  3712.     } else {
  3713.         my $cwd = CPAN::anycwd();
  3714.         $CPAN::Frontend->mydie(qq{Could not chdir from cwd[$cwd] }.
  3715.                                qq{to todir[$todir]: $!});
  3716.     }
  3717. }
  3718.  
  3719. #-> sub CPAN::Distribution::get ;
  3720. sub get {
  3721.     my($self) = @_;
  3722.   EXCUSE: {
  3723.     my @e;
  3724.     exists $self->{'build_dir'} and push @e,
  3725.         "Is already unwrapped into directory $self->{'build_dir'}";
  3726.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  3727.     }
  3728.     my $sub_wd = CPAN::anycwd(); # for cleaning up as good as possible
  3729.  
  3730.     #
  3731.     # Get the file on local disk
  3732.     #
  3733.  
  3734.     my($local_file);
  3735.     my($local_wanted) =
  3736.         MM->catfile(
  3737.                     $CPAN::Config->{keep_source_where},
  3738.                     "authors",
  3739.                     "id",
  3740.                     split("/",$self->id)
  3741.                    );
  3742.  
  3743.     $self->debug("Doing localize") if $CPAN::DEBUG;
  3744.     unless ($local_file =
  3745.             CPAN::FTP->localize("authors/id/$self->{ID}",
  3746.                                 $local_wanted)) {
  3747.         my $note = "";
  3748.         if ($CPAN::Index::DATE_OF_02) {
  3749.             $note = "Note: Current database in memory was generated ".
  3750.                 "on $CPAN::Index::DATE_OF_02\n";
  3751.         }
  3752.         $CPAN::Frontend->mydie("Giving up on '$local_wanted'\n$note");
  3753.     }
  3754.     $self->debug("local_file[$local_file]") if $CPAN::DEBUG;
  3755.     $self->{localfile} = $local_file;
  3756.     return if $CPAN::Signal;
  3757.  
  3758.     #
  3759.     # Check integrity
  3760.     #
  3761.     if ($CPAN::META->has_inst("MD5")) {
  3762.     $self->debug("MD5 is installed, verifying");
  3763.     $self->verifyMD5;
  3764.     } else {
  3765.     $self->debug("MD5 is NOT installed");
  3766.     }
  3767.     return if $CPAN::Signal;
  3768.  
  3769.     #
  3770.     # Create a clean room and go there
  3771.     #
  3772.     $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new(); # unsafe meta access, ok
  3773.     my $builddir = $CPAN::META->{cachemgr}->dir; # unsafe meta access, ok
  3774.     $self->safe_chdir($builddir);
  3775.     $self->debug("Removing tmp") if $CPAN::DEBUG;
  3776.     File::Path::rmtree("tmp");
  3777.     mkdir "tmp", 0755 or Carp::croak "Couldn't mkdir tmp: $!";
  3778.     if ($CPAN::Signal){
  3779.         $self->safe_chdir($sub_wd);
  3780.         return;
  3781.     }
  3782.     $self->safe_chdir("tmp");
  3783.  
  3784.     #
  3785.     # Unpack the goods
  3786.     #
  3787.     if ($local_file =~ /(\.tar\.(gz|Z)|\.tgz)(?!\n)\Z/i){
  3788.         $self->{was_uncompressed}++ unless CPAN::Tarzip->gtest($local_file);
  3789.     $self->untar_me($local_file);
  3790.     } elsif ( $local_file =~ /\.zip(?!\n)\Z/i ) {
  3791.     $self->unzip_me($local_file);
  3792.     } elsif ( $local_file =~ /\.pm\.(gz|Z)(?!\n)\Z/) {
  3793.         $self->{was_uncompressed}++ unless CPAN::Tarzip->gtest($local_file);
  3794.     $self->pm2dir_me($local_file);
  3795.     } else {
  3796.     $self->{archived} = "NO";
  3797.         $self->safe_chdir($sub_wd);
  3798.         return;
  3799.     }
  3800.  
  3801.     # we are still in the tmp directory!
  3802.     # Let's check if the package has its own directory.
  3803.     my $dh = DirHandle->new(File::Spec->curdir)
  3804.         or Carp::croak("Couldn't opendir .: $!");
  3805.     my @readdir = grep $_ !~ /^\.\.?(?!\n)\Z/s, $dh->read; ### MAC??
  3806.     $dh->close;
  3807.     my ($distdir,$packagedir);
  3808.     if (@readdir == 1 && -d $readdir[0]) {
  3809.         $distdir = $readdir[0];
  3810.         $packagedir = MM->catdir($builddir,$distdir);
  3811.         $self->debug("packagedir[$packagedir]builddir[$builddir]distdir[$distdir]")
  3812.             if $CPAN::DEBUG;
  3813.         -d $packagedir and $CPAN::Frontend->myprint("Removing previously used ".
  3814.                                                     "$packagedir\n");
  3815.         File::Path::rmtree($packagedir);
  3816.         rename($distdir,$packagedir) or
  3817.             Carp::confess("Couldn't rename $distdir to $packagedir: $!");
  3818.         $self->debug(sprintf("renamed distdir[%s] to packagedir[%s] -e[%s]-d[%s]",
  3819.                              $distdir,
  3820.                              $packagedir,
  3821.                              -e $packagedir,
  3822.                              -d $packagedir,
  3823.                             )) if $CPAN::DEBUG;
  3824.     } else {
  3825.         my $userid = $self->cpan_userid;
  3826.         unless ($userid) {
  3827.             CPAN->debug("no userid? self[$self]");
  3828.             $userid = "anon";
  3829.         }
  3830.         my $pragmatic_dir = $userid . '000';
  3831.         $pragmatic_dir =~ s/\W_//g;
  3832.         $pragmatic_dir++ while -d "../$pragmatic_dir";
  3833.         $packagedir = MM->catdir($builddir,$pragmatic_dir);
  3834.         $self->debug("packagedir[$packagedir]") if $CPAN::DEBUG;
  3835.         File::Path::mkpath($packagedir);
  3836.         my($f);
  3837.         for $f (@readdir) { # is already without "." and ".."
  3838.             my $to = MM->catdir($packagedir,$f);
  3839.             rename($f,$to) or Carp::confess("Couldn't rename $f to $to: $!");
  3840.         }
  3841.     }
  3842.     if ($CPAN::Signal){
  3843.         $self->safe_chdir($sub_wd);
  3844.         return;
  3845.     }
  3846.  
  3847.     $self->{'build_dir'} = $packagedir;
  3848.     $self->safe_chdir(File::Spec->updir);
  3849.     File::Path::rmtree("tmp");
  3850.  
  3851.     my($mpl) = MM->catfile($packagedir,"Makefile.PL");
  3852.     my($mpl_exists) = -f $mpl;
  3853.     unless ($mpl_exists) {
  3854.         # NFS has been reported to have racing problems after the
  3855.         # renaming of a directory in some environments.
  3856.         # This trick helps.
  3857.         sleep 1;
  3858.         my $mpldh = DirHandle->new($packagedir)
  3859.             or Carp::croak("Couldn't opendir $packagedir: $!");
  3860.         $mpl_exists = grep /^Makefile\.PL$/, $mpldh->read;
  3861.         $mpldh->close;
  3862.     }
  3863.     unless ($mpl_exists) {
  3864.         $self->debug(sprintf("makefilepl[%s]anycwd[%s]",
  3865.                              $mpl,
  3866.                              CPAN::anycwd(),
  3867.                             )) if $CPAN::DEBUG;
  3868.         my($configure) = MM->catfile($packagedir,"Configure");
  3869.         if (-f $configure) {
  3870.             # do we have anything to do?
  3871.             $self->{'configure'} = $configure;
  3872.         } elsif (-f MM->catfile($packagedir,"Makefile")) {
  3873.             $CPAN::Frontend->myprint(qq{
  3874. Package comes with a Makefile and without a Makefile.PL.
  3875. We\'ll try to build it with that Makefile then.
  3876. });
  3877.             $self->{writemakefile} = "YES";
  3878.             sleep 2;
  3879.         } else {
  3880.             my $cf = $self->called_for || "unknown";
  3881.             if ($cf =~ m|/|) {
  3882.                 $cf =~ s|.*/||;
  3883.                 $cf =~ s|\W.*||;
  3884.             }
  3885.             $cf =~ s|[/\\:]||g; # risk of filesystem damage
  3886.             $cf = "unknown" unless length($cf);
  3887.             $CPAN::Frontend->myprint(qq{Package seems to come without Makefile.PL.
  3888.   (The test -f "$mpl" returned false.)
  3889.   Writing one on our own (setting NAME to $cf)\a\n});
  3890.             $self->{had_no_makefile_pl}++;
  3891.             sleep 3;
  3892.  
  3893.             # Writing our own Makefile.PL
  3894.  
  3895.             my $fh = FileHandle->new;
  3896.             $fh->open(">$mpl")
  3897.                 or Carp::croak("Could not open >$mpl: $!");
  3898.             $fh->print(
  3899. qq{# This Makefile.PL has been autogenerated by the module CPAN.pm
  3900. # because there was no Makefile.PL supplied.
  3901. # Autogenerated on: }.scalar localtime().qq{
  3902.  
  3903. use ExtUtils::MakeMaker;
  3904. WriteMakefile(NAME => q[$cf]);
  3905.  
  3906. });
  3907.             $fh->close;
  3908.         }
  3909.     }
  3910.  
  3911.     return $self;
  3912. }
  3913.  
  3914. # CPAN::Distribution::untar_me ;
  3915. sub untar_me {
  3916.     my($self,$local_file) = @_;
  3917.     $self->{archived} = "tar";
  3918.     if (CPAN::Tarzip->untar($local_file)) {
  3919.     $self->{unwrapped} = "YES";
  3920.     } else {
  3921.     $self->{unwrapped} = "NO";
  3922.     }
  3923. }
  3924.  
  3925. # CPAN::Distribution::unzip_me ;
  3926. sub unzip_me {
  3927.     my($self,$local_file) = @_;
  3928.     $self->{archived} = "zip";
  3929.     if (CPAN::Tarzip->unzip($local_file)) {
  3930.     $self->{unwrapped} = "YES";
  3931.     } else {
  3932.     $self->{unwrapped} = "NO";
  3933.     }
  3934.     return;
  3935. }
  3936.  
  3937. sub pm2dir_me {
  3938.     my($self,$local_file) = @_;
  3939.     $self->{archived} = "pm";
  3940.     my $to = File::Basename::basename($local_file);
  3941.     $to =~ s/\.(gz|Z)(?!\n)\Z//;
  3942.     if (CPAN::Tarzip->gunzip($local_file,$to)) {
  3943.     $self->{unwrapped} = "YES";
  3944.     } else {
  3945.     $self->{unwrapped} = "NO";
  3946.     }
  3947. }
  3948.  
  3949. #-> sub CPAN::Distribution::new ;
  3950. sub new {
  3951.     my($class,%att) = @_;
  3952.  
  3953.     # $CPAN::META->{cachemgr} ||= CPAN::CacheMgr->new();
  3954.  
  3955.     my $this = { %att };
  3956.     return bless $this, $class;
  3957. }
  3958.  
  3959. #-> sub CPAN::Distribution::look ;
  3960. sub look {
  3961.     my($self) = @_;
  3962.  
  3963.     if ($^O eq 'MacOS') {
  3964.       $self->ExtUtils::MM_MacOS::look;
  3965.       return;
  3966.     }
  3967.  
  3968.     if (  $CPAN::Config->{'shell'} ) {
  3969.     $CPAN::Frontend->myprint(qq{
  3970. Trying to open a subshell in the build directory...
  3971. });
  3972.     } else {
  3973.     $CPAN::Frontend->myprint(qq{
  3974. Your configuration does not define a value for subshells.
  3975. Please define it with "o conf shell <your shell>"
  3976. });
  3977.     return;
  3978.     }
  3979.     my $dist = $self->id;
  3980.     my $dir;
  3981.     unless ($dir = $self->dir) {
  3982.         $self->get;
  3983.     }
  3984.     unless ($dir ||= $self->dir) {
  3985.     $CPAN::Frontend->mywarn(qq{
  3986. Could not determine which directory to use for looking at $dist.
  3987. });
  3988.     return;
  3989.     }
  3990.     my $pwd  = CPAN::anycwd();
  3991.     $self->safe_chdir($dir);
  3992.     $CPAN::Frontend->myprint(qq{Working directory is $dir\n});
  3993.     system($CPAN::Config->{'shell'}) == 0
  3994.     or $CPAN::Frontend->mydie("Subprocess shell error");
  3995.     $self->safe_chdir($pwd);
  3996. }
  3997.  
  3998. # CPAN::Distribution::cvs_import ;
  3999. sub cvs_import {
  4000.     my($self) = @_;
  4001.     $self->get;
  4002.     my $dir = $self->dir;
  4003.  
  4004.     my $package = $self->called_for;
  4005.     my $module = $CPAN::META->instance('CPAN::Module', $package);
  4006.     my $version = $module->cpan_version;
  4007.  
  4008.     my $userid = $self->cpan_userid;
  4009.  
  4010.     my $cvs_dir = (split '/', $dir)[-1];
  4011.     $cvs_dir =~ s/-\d+[^-]+(?!\n)\Z//;
  4012.     my $cvs_root = 
  4013.       $CPAN::Config->{cvsroot} || $ENV{CVSROOT};
  4014.     my $cvs_site_perl = 
  4015.       $CPAN::Config->{cvs_site_perl} || $ENV{CVS_SITE_PERL};
  4016.     if ($cvs_site_perl) {
  4017.     $cvs_dir = "$cvs_site_perl/$cvs_dir";
  4018.     }
  4019.     my $cvs_log = qq{"imported $package $version sources"};
  4020.     $version =~ s/\./_/g;
  4021.     my @cmd = ('cvs', '-d', $cvs_root, 'import', '-m', $cvs_log,
  4022.            "$cvs_dir", $userid, "v$version");
  4023.  
  4024.     my $pwd  = CPAN::anycwd();
  4025.     chdir($dir) or $CPAN::Frontend->mydie(qq{Could not chdir to "$dir": $!});
  4026.  
  4027.     $CPAN::Frontend->myprint(qq{Working directory is $dir\n});
  4028.  
  4029.     $CPAN::Frontend->myprint(qq{@cmd\n});
  4030.     system(@cmd) == 0 or
  4031.     $CPAN::Frontend->mydie("cvs import failed");
  4032.     chdir($pwd) or $CPAN::Frontend->mydie(qq{Could not chdir to "$pwd": $!});
  4033. }
  4034.  
  4035. #-> sub CPAN::Distribution::readme ;
  4036. sub readme {
  4037.     my($self) = @_;
  4038.     my($dist) = $self->id;
  4039.     my($sans,$suffix) = $dist =~ /(.+)\.(tgz|tar[\._-]gz|tar\.Z|zip)$/;
  4040.     $self->debug("sans[$sans] suffix[$suffix]\n") if $CPAN::DEBUG;
  4041.     my($local_file);
  4042.     my($local_wanted) =
  4043.      MM->catfile(
  4044.             $CPAN::Config->{keep_source_where},
  4045.             "authors",
  4046.             "id",
  4047.             split("/","$sans.readme"),
  4048.                );
  4049.     $self->debug("Doing localize") if $CPAN::DEBUG;
  4050.     $local_file = CPAN::FTP->localize("authors/id/$sans.readme",
  4051.                       $local_wanted)
  4052.     or $CPAN::Frontend->mydie(qq{No $sans.readme found});;
  4053.  
  4054.     if ($^O eq 'MacOS') {
  4055.         ExtUtils::MM_MacOS::launch_file($local_file);
  4056.         return;
  4057.     }
  4058.  
  4059.     my $fh_pager = FileHandle->new;
  4060.     local($SIG{PIPE}) = "IGNORE";
  4061.     $fh_pager->open("|$CPAN::Config->{'pager'}")
  4062.     or die "Could not open pager $CPAN::Config->{'pager'}: $!";
  4063.     my $fh_readme = FileHandle->new;
  4064.     $fh_readme->open($local_file)
  4065.     or $CPAN::Frontend->mydie(qq{Could not open "$local_file": $!});
  4066.     $CPAN::Frontend->myprint(qq{
  4067. Displaying file
  4068.   $local_file
  4069. with pager "$CPAN::Config->{'pager'}"
  4070. });
  4071.     sleep 2;
  4072.     $fh_pager->print(<$fh_readme>);
  4073. }
  4074.  
  4075. #-> sub CPAN::Distribution::verifyMD5 ;
  4076. sub verifyMD5 {
  4077.     my($self) = @_;
  4078.   EXCUSE: {
  4079.     my @e;
  4080.     $self->{MD5_STATUS} ||= "";
  4081.     $self->{MD5_STATUS} eq "OK" and push @e, "MD5 Checksum was ok";
  4082.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4083.     }
  4084.     my($lc_want,$lc_file,@local,$basename);
  4085.     @local = split("/",$self->id);
  4086.     pop @local;
  4087.     push @local, "CHECKSUMS";
  4088.     $lc_want =
  4089.     MM->catfile($CPAN::Config->{keep_source_where},
  4090.               "authors", "id", @local);
  4091.     local($") = "/";
  4092.     if (
  4093.     -s $lc_want
  4094.     &&
  4095.     $self->MD5_check_file($lc_want)
  4096.        ) {
  4097.     return $self->{MD5_STATUS} = "OK";
  4098.     }
  4099.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  4100.                    $lc_want,1);
  4101.     unless ($lc_file) {
  4102.         $CPAN::Frontend->myprint("Trying $lc_want.gz\n");
  4103.     $local[-1] .= ".gz";
  4104.     $lc_file = CPAN::FTP->localize("authors/id/@local",
  4105.                        "$lc_want.gz",1);
  4106.     if ($lc_file) {
  4107.         $lc_file =~ s/\.gz(?!\n)\Z//;
  4108.         CPAN::Tarzip->gunzip("$lc_file.gz",$lc_file);
  4109.     } else {
  4110.         return;
  4111.     }
  4112.     }
  4113.     $self->MD5_check_file($lc_file);
  4114. }
  4115.  
  4116. #-> sub CPAN::Distribution::MD5_check_file ;
  4117. sub MD5_check_file {
  4118.     my($self,$chk_file) = @_;
  4119.     my($cksum,$file,$basename);
  4120.     $file = $self->{localfile};
  4121.     $basename = File::Basename::basename($file);
  4122.     my $fh = FileHandle->new;
  4123.     if (open $fh, $chk_file){
  4124.     local($/);
  4125.     my $eval = <$fh>;
  4126.     $eval =~ s/\015?\012/\n/g;
  4127.     close $fh;
  4128.     my($comp) = Safe->new();
  4129.     $cksum = $comp->reval($eval);
  4130.     if ($@) {
  4131.         rename $chk_file, "$chk_file.bad";
  4132.         Carp::confess($@) if $@;
  4133.     }
  4134.     } else {
  4135.     Carp::carp "Could not open $chk_file for reading";
  4136.     }
  4137.  
  4138.     if (exists $cksum->{$basename}{md5}) {
  4139.     $self->debug("Found checksum for $basename:" .
  4140.              "$cksum->{$basename}{md5}\n") if $CPAN::DEBUG;
  4141.  
  4142.     open($fh, $file);
  4143.     binmode $fh;
  4144.     my $eq = $self->eq_MD5($fh,$cksum->{$basename}{'md5'});
  4145.     $fh->close;
  4146.     $fh = CPAN::Tarzip->TIEHANDLE($file);
  4147.  
  4148.     unless ($eq) {
  4149.       # had to inline it, when I tied it, the tiedness got lost on
  4150.       # the call to eq_MD5. (Jan 1998)
  4151.       my $md5 = MD5->new;
  4152.       my($data,$ref);
  4153.       $ref = \$data;
  4154.       while ($fh->READ($ref, 4096) > 0){
  4155.         $md5->add($data);
  4156.       }
  4157.       my $hexdigest = $md5->hexdigest;
  4158.       $eq += $hexdigest eq $cksum->{$basename}{'md5-ungz'};
  4159.     }
  4160.  
  4161.     if ($eq) {
  4162.       $CPAN::Frontend->myprint("Checksum for $file ok\n");
  4163.       return $self->{MD5_STATUS} = "OK";
  4164.     } else {
  4165.         $CPAN::Frontend->myprint(qq{\nChecksum mismatch for }.
  4166.                      qq{distribution file. }.
  4167.                      qq{Please investigate.\n\n}.
  4168.                      $self->as_string,
  4169.                      $CPAN::META->instance(
  4170.                                'CPAN::Author',
  4171.                                $self->cpan_userid
  4172.                               )->as_string);
  4173.  
  4174.         my $wrap = qq{I\'d recommend removing $file. Its MD5
  4175. checksum is incorrect. Maybe you have configured your 'urllist' with
  4176. a bad URL. Please check this array with 'o conf urllist', and
  4177. retry.};
  4178.  
  4179.             $CPAN::Frontend->mydie(Text::Wrap::wrap("","",$wrap));
  4180.  
  4181.             # former versions just returned here but this seems a
  4182.             # serious threat that deserves a die
  4183.  
  4184.         # $CPAN::Frontend->myprint("\n\n");
  4185.         # sleep 3;
  4186.         # return;
  4187.     }
  4188.     # close $fh if fileno($fh);
  4189.     } else {
  4190.     $self->{MD5_STATUS} ||= "";
  4191.     if ($self->{MD5_STATUS} eq "NIL") {
  4192.         $CPAN::Frontend->mywarn(qq{
  4193. Warning: No md5 checksum for $basename in $chk_file.
  4194.  
  4195. The cause for this may be that the file is very new and the checksum
  4196. has not yet been calculated, but it may also be that something is
  4197. going awry right now.
  4198. });
  4199.             my $answer = ExtUtils::MakeMaker::prompt("Proceed?", "yes");
  4200.             $answer =~ /^\s*y/i or $CPAN::Frontend->mydie("Aborted.");
  4201.     }
  4202.     $self->{MD5_STATUS} = "NIL";
  4203.     return;
  4204.     }
  4205. }
  4206.  
  4207. #-> sub CPAN::Distribution::eq_MD5 ;
  4208. sub eq_MD5 {
  4209.     my($self,$fh,$expectMD5) = @_;
  4210.     my $md5 = MD5->new;
  4211.     my($data);
  4212.     while (read($fh, $data, 4096)){
  4213.       $md5->add($data);
  4214.     }
  4215.     # $md5->addfile($fh);
  4216.     my $hexdigest = $md5->hexdigest;
  4217.     # warn "fh[$fh] hex[$hexdigest] aexp[$expectMD5]";
  4218.     $hexdigest eq $expectMD5;
  4219. }
  4220.  
  4221. #-> sub CPAN::Distribution::force ;
  4222.  
  4223. # Both modules and distributions know if "force" is in effect by
  4224. # autoinspection, not by inspecting a global variable. One of the
  4225. # reason why this was chosen to work that way was the treatment of
  4226. # dependencies. They should not autpomatically inherit the force
  4227. # status. But this has the downside that ^C and die() will return to
  4228. # the prompt but will not be able to reset the force_update
  4229. # attributes. We try to correct for it currently in the read_metadata
  4230. # routine, and immediately before we check for a Signal. I hope this
  4231. # works out in one of v1.57_53ff
  4232.  
  4233. sub force {
  4234.   my($self, $method) = @_;
  4235.   for my $att (qw(
  4236.   MD5_STATUS archived build_dir localfile make install unwrapped
  4237.   writemakefile
  4238.  )) {
  4239.     delete $self->{$att};
  4240.   }
  4241.   if ($method && $method eq "install") {
  4242.     $self->{"force_update"}++; # name should probably have been force_install
  4243.   }
  4244. }
  4245.  
  4246. #-> sub CPAN::Distribution::unforce ;
  4247. sub unforce {
  4248.   my($self) = @_;
  4249.   delete $self->{'force_update'};
  4250. }
  4251.  
  4252. #-> sub CPAN::Distribution::isa_perl ;
  4253. sub isa_perl {
  4254.   my($self) = @_;
  4255.   my $file = File::Basename::basename($self->id);
  4256.   if ($file =~ m{ ^ perl
  4257.                   -?
  4258.           (5)
  4259.           ([._-])
  4260.           (
  4261.                    \d{3}(_[0-4][0-9])?
  4262.                    |
  4263.                    \d*[24680]\.\d+
  4264.                   )
  4265.           \.tar[._-]gz
  4266.           (?!\n)\Z
  4267.         }xs){
  4268.     return "$1.$3";
  4269.   } elsif ($self->cpan_comment
  4270.            &&
  4271.            $self->cpan_comment =~ /isa_perl\(.+?\)/){
  4272.     return $1;
  4273.   }
  4274. }
  4275.  
  4276. #-> sub CPAN::Distribution::perl ;
  4277. sub perl {
  4278.     my($self) = @_;
  4279.     my($perl) = MM->file_name_is_absolute($^X) ? $^X : "";
  4280.     my $pwd  = CPAN::anycwd();
  4281.     my $candidate = MM->catfile($pwd,$^X);
  4282.     $perl ||= $candidate if MM->maybe_command($candidate);
  4283.     unless ($perl) {
  4284.     my ($component,$perl_name);
  4285.       DIST_PERLNAME: foreach $perl_name ($^X, 'perl', 'perl5', "perl$]") {
  4286.         PATH_COMPONENT: foreach $component (MM->path(),
  4287.                         $Config::Config{'binexp'}) {
  4288.           next unless defined($component) && $component;
  4289.           my($abs) = MM->catfile($component,$perl_name);
  4290.           if (MM->maybe_command($abs)) {
  4291.               $perl = $abs;
  4292.               last DIST_PERLNAME;
  4293.           }
  4294.           }
  4295.       }
  4296.     }
  4297.     $perl;
  4298. }
  4299.  
  4300. #-> sub CPAN::Distribution::make ;
  4301. sub make {
  4302.     my($self) = @_;
  4303.     $CPAN::Frontend->myprint(sprintf "Running make for %s\n", $self->id);
  4304.     # Emergency brake if they said install Pippi and get newest perl
  4305.     if ($self->isa_perl) {
  4306.       if (
  4307.       $self->called_for ne $self->id &&
  4308.           ! $self->{force_update}
  4309.      ) {
  4310.         # if we die here, we break bundles
  4311.     $CPAN::Frontend->mywarn(sprintf qq{
  4312. The most recent version "%s" of the module "%s"
  4313. comes with the current version of perl (%s).
  4314. I\'ll build that only if you ask for something like
  4315.     force install %s
  4316. or
  4317.     install %s
  4318. },
  4319.                    $CPAN::META->instance(
  4320.                              'CPAN::Module',
  4321.                              $self->called_for
  4322.                             )->cpan_version,
  4323.                    $self->called_for,
  4324.                    $self->isa_perl,
  4325.                    $self->called_for,
  4326.                    $self->id);
  4327.         sleep 5; return;
  4328.       }
  4329.     }
  4330.     $self->get;
  4331.   EXCUSE: {
  4332.     my @e;
  4333.     $self->{archived} eq "NO" and push @e,
  4334.     "Is neither a tar nor a zip archive.";
  4335.  
  4336.     $self->{unwrapped} eq "NO" and push @e,
  4337.     "had problems unarchiving. Please build manually";
  4338.  
  4339.     exists $self->{writemakefile} &&
  4340.         $self->{writemakefile} =~ m/ ^ NO\s* ( .* ) /sx and push @e,
  4341.         $1 || "Had some problem writing Makefile";
  4342.  
  4343.     defined $self->{'make'} and push @e,
  4344.             "Has already been processed within this session";
  4345.  
  4346.         exists $self->{later} and length($self->{later}) and
  4347.             push @e, $self->{later};
  4348.  
  4349.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4350.     }
  4351.     $CPAN::Frontend->myprint("\n  CPAN.pm: Going to build ".$self->id."\n\n");
  4352.     my $builddir = $self->dir;
  4353.     chdir $builddir or Carp::croak("Couldn't chdir $builddir: $!");
  4354.     $self->debug("Changed directory to $builddir") if $CPAN::DEBUG;
  4355.  
  4356.     if ($^O eq 'MacOS') {
  4357.         ExtUtils::MM_MacOS::make($self);
  4358.         return;
  4359.     }
  4360.  
  4361.     my $system;
  4362.     if ($self->{'configure'}) {
  4363.       $system = $self->{'configure'};
  4364.     } else {
  4365.     my($perl) = $self->perl or die "Couldn\'t find executable perl\n";
  4366.     my $switch = "";
  4367. # This needs a handler that can be turned on or off:
  4368. #    $switch = "-MExtUtils::MakeMaker ".
  4369. #        "-Mops=:default,:filesys_read,:filesys_open,require,chdir"
  4370. #        if $] > 5.00310;
  4371.     $system = "$perl $switch Makefile.PL $CPAN::Config->{makepl_arg}";
  4372.     }
  4373.     unless (exists $self->{writemakefile}) {
  4374.     local($SIG{ALRM}) = sub { die "inactivity_timeout reached\n" };
  4375.     my($ret,$pid);
  4376.     $@ = "";
  4377.     if ($CPAN::Config->{inactivity_timeout}) {
  4378.         eval {
  4379.         alarm $CPAN::Config->{inactivity_timeout};
  4380.         local $SIG{CHLD}; # = sub { wait };
  4381.         if (defined($pid = fork)) {
  4382.             if ($pid) { #parent
  4383.             # wait;
  4384.             waitpid $pid, 0;
  4385.             } else {    #child
  4386.               # note, this exec isn't necessary if
  4387.               # inactivity_timeout is 0. On the Mac I'd
  4388.               # suggest, we set it always to 0.
  4389.               exec $system;
  4390.             }
  4391.         } else {
  4392.             $CPAN::Frontend->myprint("Cannot fork: $!");
  4393.             return;
  4394.         }
  4395.         };
  4396.         alarm 0;
  4397.         if ($@){
  4398.         kill 9, $pid;
  4399.         waitpid $pid, 0;
  4400.         $CPAN::Frontend->myprint($@);
  4401.         $self->{writemakefile} = "NO $@";
  4402.         $@ = "";
  4403.         return;
  4404.         }
  4405.     } else {
  4406.       $ret = system($system);
  4407.       if ($ret != 0) {
  4408.         $self->{writemakefile} = "NO Makefile.PL returned status $ret";
  4409.         return;
  4410.       }
  4411.     }
  4412.     if (-f "Makefile") {
  4413.       $self->{writemakefile} = "YES";
  4414.           delete $self->{make_clean}; # if cleaned before, enable next
  4415.     } else {
  4416.       $self->{writemakefile} =
  4417.           qq{NO Makefile.PL refused to write a Makefile.};
  4418.       # It's probably worth to record the reason, so let's retry
  4419.       # local $/;
  4420.       # my $fh = IO::File->new("$system |"); # STDERR? STDIN?
  4421.       # $self->{writemakefile} .= <$fh>;
  4422.     }
  4423.     }
  4424.     if ($CPAN::Signal){
  4425.       delete $self->{force_update};
  4426.       return;
  4427.     }
  4428.     if (my @prereq = $self->unsat_prereq){
  4429.       return 1 if $self->follow_prereqs(@prereq); # signal success to the queuerunner
  4430.     }
  4431.     $system = join " ", $CPAN::Config->{'make'}, $CPAN::Config->{make_arg};
  4432.     if (system($system) == 0) {
  4433.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4434.      $self->{'make'} = "YES";
  4435.     } else {
  4436.      $self->{writemakefile} ||= "YES";
  4437.      $self->{'make'} = "NO";
  4438.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4439.     }
  4440. }
  4441.  
  4442. sub follow_prereqs {
  4443.     my($self) = shift;
  4444.     my(@prereq) = @_;
  4445.     my $id = $self->id;
  4446.     $CPAN::Frontend->myprint("---- Unsatisfied dependencies detected ".
  4447.                              "during [$id] -----\n");
  4448.  
  4449.     for my $p (@prereq) {
  4450.     $CPAN::Frontend->myprint("    $p\n");
  4451.     }
  4452.     my $follow = 0;
  4453.     if ($CPAN::Config->{prerequisites_policy} eq "follow") {
  4454.     $follow = 1;
  4455.     } elsif ($CPAN::Config->{prerequisites_policy} eq "ask") {
  4456.     require ExtUtils::MakeMaker;
  4457.     my $answer = ExtUtils::MakeMaker::prompt(
  4458. "Shall I follow them and prepend them to the queue
  4459. of modules we are processing right now?", "yes");
  4460.     $follow = $answer =~ /^\s*y/i;
  4461.     } else {
  4462.     local($") = ", ";
  4463.     $CPAN::Frontend->
  4464.             myprint("  Ignoring dependencies on modules @prereq\n");
  4465.     }
  4466.     if ($follow) {
  4467.         # color them as dirty
  4468.         for my $p (@prereq) {
  4469.             CPAN::Shell->expandany($p)->color_cmd_tmps(0,1);
  4470.         }
  4471.         CPAN::Queue->jumpqueue(@prereq,$id); # queue them and requeue yourself
  4472.         $self->{later} = "Delayed until after prerequisites";
  4473.         return 1; # signal success to the queuerunner
  4474.     }
  4475. }
  4476.  
  4477. #-> sub CPAN::Distribution::unsat_prereq ;
  4478. sub unsat_prereq {
  4479.     my($self) = @_;
  4480.     my $prereq_pm = $self->prereq_pm or return;
  4481.     my(@need);
  4482.   NEED: while (my($need_module, $need_version) = each %$prereq_pm) {
  4483.         my $nmo = $CPAN::META->instance("CPAN::Module",$need_module);
  4484.         # we were too demanding:
  4485.         next if $nmo->uptodate;
  4486.  
  4487.         # if they have not specified a version, we accept any installed one
  4488.         if (not defined $need_version or
  4489.            $need_version == 0 or
  4490.            $need_version eq "undef") {
  4491.             next if defined $nmo->inst_file;
  4492.         }
  4493.  
  4494.         # We only want to install prereqs if either they're not installed
  4495.         # or if the installed version is too old. We cannot omit this
  4496.         # check, because if 'force' is in effect, nobody else will check.
  4497.         {
  4498.             local($^W) = 0;
  4499.             if (
  4500.                 defined $nmo->inst_file &&
  4501.                 ! CPAN::Version->vgt($need_version, $nmo->inst_version)
  4502.                ){
  4503.                 CPAN->debug(sprintf "id[%s]inst_file[%s]inst_version[%s]need_version[%s]",
  4504.                             $nmo->id,
  4505.                             $nmo->inst_file,
  4506.                             $nmo->inst_version,
  4507.                             CPAN::Version->readable($need_version)
  4508.                            );
  4509.                 next NEED;
  4510.             }
  4511.         }
  4512.  
  4513.         if ($self->{sponsored_mods}{$need_module}++){
  4514.             # We have already sponsored it and for some reason it's still
  4515.             # not available. So we do nothing. Or what should we do?
  4516.             # if we push it again, we have a potential infinite loop
  4517.             next;
  4518.         }
  4519.         push @need, $need_module;
  4520.     }
  4521.     @need;
  4522. }
  4523.  
  4524. #-> sub CPAN::Distribution::prereq_pm ;
  4525. sub prereq_pm {
  4526.   my($self) = @_;
  4527.   return $self->{prereq_pm} if
  4528.       exists $self->{prereq_pm_detected} && $self->{prereq_pm_detected};
  4529.   return unless $self->{writemakefile}; # no need to have succeeded
  4530.                                         # but we must have run it
  4531.   my $build_dir = $self->{build_dir} or die "Panic: no build_dir?";
  4532.   my $makefile = File::Spec->catfile($build_dir,"Makefile");
  4533.   my(%p) = ();
  4534.   my $fh;
  4535.   if (-f $makefile
  4536.       and
  4537.       $fh = FileHandle->new("<$makefile\0")) {
  4538.  
  4539.       local($/) = "\n";
  4540.  
  4541.       #  A.Speer @p -> %p, where %p is $p{Module::Name}=Required_Version
  4542.       while (<$fh>) {
  4543.           last if /MakeMaker post_initialize section/;
  4544.           my($p) = m{^[\#]
  4545.          \s+PREREQ_PM\s+=>\s+(.+)
  4546.          }x;
  4547.           next unless $p;
  4548.           # warn "Found prereq expr[$p]";
  4549.  
  4550.           #  Regexp modified by A.Speer to remember actual version of file
  4551.           #  PREREQ_PM hash key wants, then add to
  4552.           while ( $p =~ m/(?:\s)([\w\:]+)=>q\[(.*?)\],?/g ){
  4553.               # In case a prereq is mentioned twice, complain.
  4554.               if ( defined $p{$1} ) {
  4555.                   warn "Warning: PREREQ_PM mentions $1 more than once, last mention wins";
  4556.               }
  4557.               $p{$1} = $2;
  4558.           }
  4559.           last;
  4560.       }
  4561.   }
  4562.   $self->{prereq_pm_detected}++;
  4563.   return $self->{prereq_pm} = \%p;
  4564. }
  4565.  
  4566. #-> sub CPAN::Distribution::test ;
  4567. sub test {
  4568.     my($self) = @_;
  4569.     $self->make;
  4570.     if ($CPAN::Signal){
  4571.       delete $self->{force_update};
  4572.       return;
  4573.     }
  4574.     $CPAN::Frontend->myprint("Running make test\n");
  4575.     if (my @prereq = $self->unsat_prereq){
  4576.       return 1 if $self->follow_prereqs(@prereq); # signal success to the queuerunner
  4577.     }
  4578.   EXCUSE: {
  4579.     my @e;
  4580.     exists $self->{make} or exists $self->{later} or push @e,
  4581.     "Make had some problems, maybe interrupted? Won't test";
  4582.  
  4583.     exists $self->{'make'} and
  4584.         $self->{'make'} eq 'NO' and
  4585.         push @e, "Can't test without successful make";
  4586.  
  4587.     exists $self->{build_dir} or push @e, "Has no own directory";
  4588.         $self->{badtestcnt} ||= 0;
  4589.         $self->{badtestcnt} > 0 and
  4590.             push @e, "Won't repeat unsuccessful test during this command";
  4591.  
  4592.         exists $self->{later} and length($self->{later}) and
  4593.             push @e, $self->{later};
  4594.  
  4595.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4596.     }
  4597.     chdir $self->{'build_dir'} or
  4598.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4599.     $self->debug("Changed directory to $self->{'build_dir'}")
  4600.     if $CPAN::DEBUG;
  4601.  
  4602.     if ($^O eq 'MacOS') {
  4603.         ExtUtils::MM_MacOS::make_test($self);
  4604.         return;
  4605.     }
  4606.  
  4607.     my $system = join " ", $CPAN::Config->{'make'}, "test";
  4608.     if (system($system) == 0) {
  4609.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4610.      $self->{make_test} = "YES";
  4611.     } else {
  4612.      $self->{make_test} = "NO";
  4613.          $self->{badtestcnt}++;
  4614.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4615.     }
  4616. }
  4617.  
  4618. #-> sub CPAN::Distribution::clean ;
  4619. sub clean {
  4620.     my($self) = @_;
  4621.     $CPAN::Frontend->myprint("Running make clean\n");
  4622.   EXCUSE: {
  4623.     my @e;
  4624.         exists $self->{make_clean} and $self->{make_clean} eq "YES" and
  4625.             push @e, "make clean already called once";
  4626.     exists $self->{build_dir} or push @e, "Has no own directory";
  4627.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4628.     }
  4629.     chdir $self->{'build_dir'} or
  4630.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4631.     $self->debug("Changed directory to $self->{'build_dir'}") if $CPAN::DEBUG;
  4632.  
  4633.     if ($^O eq 'MacOS') {
  4634.         ExtUtils::MM_MacOS::make_clean($self);
  4635.         return;
  4636.     }
  4637.  
  4638.     my $system = join " ", $CPAN::Config->{'make'}, "clean";
  4639.     if (system($system) == 0) {
  4640.       $CPAN::Frontend->myprint("  $system -- OK\n");
  4641.  
  4642.       # $self->force;
  4643.  
  4644.       # Jost Krieger pointed out that this "force" was wrong because
  4645.       # it has the effect that the next "install" on this distribution
  4646.       # will untar everything again. Instead we should bring the
  4647.       # object's state back to where it is after untarring.
  4648.  
  4649.       delete $self->{force_update};
  4650.       delete $self->{install};
  4651.       delete $self->{writemakefile};
  4652.       delete $self->{make};
  4653.       delete $self->{make_test}; # no matter if yes or no, tests must be redone
  4654.       $self->{make_clean} = "YES";
  4655.  
  4656.     } else {
  4657.       # Hmmm, what to do if make clean failed?
  4658.  
  4659.       $CPAN::Frontend->myprint(qq{  $system -- NOT OK
  4660.  
  4661. make clean did not succeed, marking directory as unusable for further work.
  4662. });
  4663.       $self->force("make"); # so that this directory won't be used again
  4664.  
  4665.     }
  4666. }
  4667.  
  4668. #-> sub CPAN::Distribution::install ;
  4669. sub install {
  4670.     my($self) = @_;
  4671.     $self->test;
  4672.     if ($CPAN::Signal){
  4673.       delete $self->{force_update};
  4674.       return;
  4675.     }
  4676.     $CPAN::Frontend->myprint("Running make install\n");
  4677.   EXCUSE: {
  4678.     my @e;
  4679.     exists $self->{build_dir} or push @e, "Has no own directory";
  4680.  
  4681.     exists $self->{make} or exists $self->{later} or push @e,
  4682.     "Make had some problems, maybe interrupted? Won't install";
  4683.  
  4684.     exists $self->{'make'} and
  4685.         $self->{'make'} eq 'NO' and
  4686.         push @e, "make had returned bad status, install seems impossible";
  4687.  
  4688.     push @e, "make test had returned bad status, ".
  4689.         "won't install without force"
  4690.         if exists $self->{'make_test'} and
  4691.         $self->{'make_test'} eq 'NO' and
  4692.         ! $self->{'force_update'};
  4693.  
  4694.     exists $self->{'install'} and push @e,
  4695.     $self->{'install'} eq "YES" ?
  4696.         "Already done" : "Already tried without success";
  4697.  
  4698.         exists $self->{later} and length($self->{later}) and
  4699.             push @e, $self->{later};
  4700.  
  4701.     $CPAN::Frontend->myprint(join "", map {"  $_\n"} @e) and return if @e;
  4702.     }
  4703.     chdir $self->{'build_dir'} or
  4704.     Carp::croak("Couldn't chdir to $self->{'build_dir'}");
  4705.     $self->debug("Changed directory to $self->{'build_dir'}")
  4706.     if $CPAN::DEBUG;
  4707.  
  4708.     if ($^O eq 'MacOS') {
  4709.         ExtUtils::MM_MacOS::make_install($self);
  4710.         return;
  4711.     }
  4712.  
  4713.     my $system = join(" ", $CPAN::Config->{'make'},
  4714.               "install", $CPAN::Config->{make_install_arg});
  4715.     my($stderr) = $^O =~ /Win/i ? "" : " 2>&1 ";
  4716.     my($pipe) = FileHandle->new("$system $stderr |");
  4717.     my($makeout) = "";
  4718.     while (<$pipe>){
  4719.     $CPAN::Frontend->myprint($_);
  4720.     $makeout .= $_;
  4721.     }
  4722.     $pipe->close;
  4723.     if ($?==0) {
  4724.      $CPAN::Frontend->myprint("  $system -- OK\n");
  4725.      return $self->{'install'} = "YES";
  4726.     } else {
  4727.      $self->{'install'} = "NO";
  4728.      $CPAN::Frontend->myprint("  $system -- NOT OK\n");
  4729.      if ($makeout =~ /permission/s && $> > 0) {
  4730.          $CPAN::Frontend->myprint(qq{    You may have to su }.
  4731.                       qq{to root to install the package\n});
  4732.      }
  4733.     }
  4734.     delete $self->{force_update};
  4735. }
  4736.  
  4737. #-> sub CPAN::Distribution::dir ;
  4738. sub dir {
  4739.     shift->{'build_dir'};
  4740. }
  4741.  
  4742. package CPAN::Bundle;
  4743.  
  4744. sub undelay {
  4745.     my $self = shift;
  4746.     delete $self->{later};
  4747.     for my $c ( $self->contains ) {
  4748.         my $obj = CPAN::Shell->expandany($c) or next;
  4749.         $obj->undelay;
  4750.     }
  4751. }
  4752.  
  4753. #-> sub CPAN::Bundle::color_cmd_tmps ;
  4754. sub color_cmd_tmps {
  4755.     my($self) = shift;
  4756.     my($depth) = shift || 0;
  4757.     my($color) = shift || 0;
  4758.     # a module needs to recurse to its cpan_file, a distribution needs
  4759.     # to recurse into its prereq_pms, a bundle needs to recurse into its modules
  4760.  
  4761.     return if exists $self->{incommandcolor}
  4762.         && $self->{incommandcolor}==$color;
  4763.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  4764.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  4765.                                    $depth,
  4766.                                    $self,
  4767.                                    $self->id
  4768.                                   )) if $depth>=100;
  4769.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  4770.  
  4771.     for my $c ( $self->contains ) {
  4772.         my $obj = CPAN::Shell->expandany($c) or next;
  4773.         CPAN->debug("c[$c]obj[$obj]") if $CPAN::DEBUG;
  4774.         $obj->color_cmd_tmps($depth+1,$color);
  4775.     }
  4776.     if ($color==0) {
  4777.         delete $self->{badtestcnt};
  4778.     }
  4779.     $self->{incommandcolor} = $color;
  4780. }
  4781.  
  4782. #-> sub CPAN::Bundle::as_string ;
  4783. sub as_string {
  4784.     my($self) = @_;
  4785.     $self->contains;
  4786.     # following line must be "=", not "||=" because we have a moving target
  4787.     $self->{INST_VERSION} = $self->inst_version;
  4788.     return $self->SUPER::as_string;
  4789. }
  4790.  
  4791. #-> sub CPAN::Bundle::contains ;
  4792. sub contains {
  4793.     my($self) = @_;
  4794.     my($inst_file) = $self->inst_file || "";
  4795.     my($id) = $self->id;
  4796.     $self->debug("inst_file[$inst_file]id[$id]") if $CPAN::DEBUG;
  4797.     unless ($inst_file) {
  4798.         # Try to get at it in the cpan directory
  4799.         $self->debug("no inst_file") if $CPAN::DEBUG;
  4800.         my $cpan_file;
  4801.         $CPAN::Frontend->mydie("I don't know a bundle with ID $id\n") unless
  4802.               $cpan_file = $self->cpan_file;
  4803.         if ($cpan_file eq "N/A") {
  4804.             $CPAN::Frontend->mydie("Bundle $id not found on disk and not on CPAN.
  4805.   Maybe stale symlink? Maybe removed during session? Giving up.\n");
  4806.         }
  4807.         my $dist = $CPAN::META->instance('CPAN::Distribution',
  4808.                                          $self->cpan_file);
  4809.         $dist->get;
  4810.         $self->debug($dist->as_string) if $CPAN::DEBUG;
  4811.         my($todir) = $CPAN::Config->{'cpan_home'};
  4812.         my(@me,$from,$to,$me);
  4813.         @me = split /::/, $self->id;
  4814.         $me[-1] .= ".pm";
  4815.         $me = MM->catfile(@me);
  4816.         $from = $self->find_bundle_file($dist->{'build_dir'},$me);
  4817.         $to = MM->catfile($todir,$me);
  4818.         File::Path::mkpath(File::Basename::dirname($to));
  4819.         File::Copy::copy($from, $to)
  4820.               or Carp::confess("Couldn't copy $from to $to: $!");
  4821.         $inst_file = $to;
  4822.     }
  4823.     my @result;
  4824.     my $fh = FileHandle->new;
  4825.     local $/ = "\n";
  4826.     open($fh,$inst_file) or die "Could not open '$inst_file': $!";
  4827.     my $in_cont = 0;
  4828.     $self->debug("inst_file[$inst_file]") if $CPAN::DEBUG;
  4829.     while (<$fh>) {
  4830.         $in_cont = m/^=(?!head1\s+CONTENTS)/ ? 0 :
  4831.             m/^=head1\s+CONTENTS/ ? 1 : $in_cont;
  4832.         next unless $in_cont;
  4833.         next if /^=/;
  4834.         s/\#.*//;
  4835.         next if /^\s+$/;
  4836.         chomp;
  4837.         push @result, (split " ", $_, 2)[0];
  4838.     }
  4839.     close $fh;
  4840.     delete $self->{STATUS};
  4841.     $self->{CONTAINS} = \@result;
  4842.     $self->debug("CONTAINS[@result]") if $CPAN::DEBUG;
  4843.     unless (@result) {
  4844.         $CPAN::Frontend->mywarn(qq{
  4845. The bundle file "$inst_file" may be a broken
  4846. bundlefile. It seems not to contain any bundle definition.
  4847. Please check the file and if it is bogus, please delete it.
  4848. Sorry for the inconvenience.
  4849. });
  4850.     }
  4851.     @result;
  4852. }
  4853.  
  4854. #-> sub CPAN::Bundle::find_bundle_file
  4855. sub find_bundle_file {
  4856.     my($self,$where,$what) = @_;
  4857.     $self->debug("where[$where]what[$what]") if $CPAN::DEBUG;
  4858. ### The following two lines let CPAN.pm become Bundle/CPAN.pm :-(
  4859. ###    my $bu = MM->catfile($where,$what);
  4860. ###    return $bu if -f $bu;
  4861.     my $manifest = MM->catfile($where,"MANIFEST");
  4862.     unless (-f $manifest) {
  4863.     require ExtUtils::Manifest;
  4864.     my $cwd = CPAN::anycwd();
  4865.     chdir $where or $CPAN::Frontend->mydie(qq{Could not chdir to "$where": $!});
  4866.     ExtUtils::Manifest::mkmanifest();
  4867.     chdir $cwd or $CPAN::Frontend->mydie(qq{Could not chdir to "$cwd": $!});
  4868.     }
  4869.     my $fh = FileHandle->new($manifest)
  4870.     or Carp::croak("Couldn't open $manifest: $!");
  4871.     local($/) = "\n";
  4872.     my $what2 = $what;
  4873.     if ($^O eq 'MacOS') {
  4874.       $what =~ s/^://;
  4875.       $what2 =~ tr|:|/|;
  4876.       $what2 =~ s/:Bundle://;
  4877.       $what2 =~ tr|:|/|;
  4878.     } else {
  4879.     $what2 =~ s|Bundle[/\\]||;
  4880.     }
  4881.     my $bu;
  4882.     while (<$fh>) {
  4883.     next if /^\s*\#/;
  4884.     my($file) = /(\S+)/;
  4885.     if ($file =~ m|\Q$what\E$|) {
  4886.         $bu = $file;
  4887.         # return MM->catfile($where,$bu); # bad
  4888.         last;
  4889.     }
  4890.     # retry if she managed to
  4891.     # have no Bundle directory
  4892.     $bu = $file if $file =~ m|\Q$what2\E$|;
  4893.     }
  4894.     $bu =~ tr|/|:| if $^O eq 'MacOS';
  4895.     return MM->catfile($where, $bu) if $bu;
  4896.     Carp::croak("Couldn't find a Bundle file in $where");
  4897. }
  4898.  
  4899. # needs to work quite differently from Module::inst_file because of
  4900. # cpan_home/Bundle/ directory and the possibility that we have
  4901. # shadowing effect. As it makes no sense to take the first in @INC for
  4902. # Bundles, we parse them all for $VERSION and take the newest.
  4903.  
  4904. #-> sub CPAN::Bundle::inst_file ;
  4905. sub inst_file {
  4906.     my($self) = @_;
  4907.     my($inst_file);
  4908.     my(@me);
  4909.     @me = split /::/, $self->id;
  4910.     $me[-1] .= ".pm";
  4911.     my($incdir,$bestv);
  4912.     foreach $incdir ($CPAN::Config->{'cpan_home'},@INC) {
  4913.         my $bfile = MM->catfile($incdir, @me);
  4914.         CPAN->debug("bfile[$bfile]") if $CPAN::DEBUG;
  4915.         next unless -f $bfile;
  4916.         my $foundv = MM->parse_version($bfile);
  4917.         if (!$bestv || CPAN::Version->vgt($foundv,$bestv)) {
  4918.             $self->{INST_FILE} = $bfile;
  4919.             $self->{INST_VERSION} = $bestv = $foundv;
  4920.         }
  4921.     }
  4922.     $self->{INST_FILE};
  4923. }
  4924.  
  4925. #-> sub CPAN::Bundle::inst_version ;
  4926. sub inst_version {
  4927.     my($self) = @_;
  4928.     $self->inst_file; # finds INST_VERSION as side effect
  4929.     $self->{INST_VERSION};
  4930. }
  4931.  
  4932. #-> sub CPAN::Bundle::rematein ;
  4933. sub rematein {
  4934.     my($self,$meth) = @_;
  4935.     $self->debug("self[$self] meth[$meth]") if $CPAN::DEBUG;
  4936.     my($id) = $self->id;
  4937.     Carp::croak "Can't $meth $id, don't have an associated bundle file. :-(\n"
  4938.     unless $self->inst_file || $self->cpan_file;
  4939.     my($s,%fail);
  4940.     for $s ($self->contains) {
  4941.     my($type) = $s =~ m|/| ? 'CPAN::Distribution' :
  4942.         $s =~ m|^Bundle::| ? 'CPAN::Bundle' : 'CPAN::Module';
  4943.     if ($type eq 'CPAN::Distribution') {
  4944.         $CPAN::Frontend->mywarn(qq{
  4945. The Bundle }.$self->id.qq{ contains
  4946. explicitly a file $s.
  4947. });
  4948.         sleep 3;
  4949.     }
  4950.     # possibly noisy action:
  4951.         $self->debug("type[$type] s[$s]") if $CPAN::DEBUG;
  4952.     my $obj = $CPAN::META->instance($type,$s);
  4953.     $obj->$meth();
  4954.         if ($obj->isa(CPAN::Bundle)
  4955.             &&
  4956.             exists $obj->{install_failed}
  4957.             &&
  4958.             ref($obj->{install_failed}) eq "HASH"
  4959.            ) {
  4960.           for (keys %{$obj->{install_failed}}) {
  4961.             $self->{install_failed}{$_} = undef; # propagate faiure up
  4962.                                                  # to me in a
  4963.                                                  # recursive call
  4964.             $fail{$s} = 1; # the bundle itself may have succeeded but
  4965.                            # not all children
  4966.           }
  4967.         } else {
  4968.           my $success;
  4969.           $success = $obj->can("uptodate") ? $obj->uptodate : 0;
  4970.           $success ||= $obj->{'install'} && $obj->{'install'} eq "YES";
  4971.           if ($success) {
  4972.             delete $self->{install_failed}{$s};
  4973.           } else {
  4974.             $fail{$s} = 1;
  4975.           }
  4976.         }
  4977.     }
  4978.  
  4979.     # recap with less noise
  4980.     if ( $meth eq "install" ) {
  4981.     if (%fail) {
  4982.         require Text::Wrap;
  4983.         my $raw = sprintf(qq{Bundle summary:
  4984. The following items in bundle %s had installation problems:},
  4985.                   $self->id
  4986.                  );
  4987.         $CPAN::Frontend->myprint(Text::Wrap::fill("","",$raw));
  4988.         $CPAN::Frontend->myprint("\n");
  4989.         my $paragraph = "";
  4990.             my %reported;
  4991.         for $s ($self->contains) {
  4992.               if ($fail{$s}){
  4993.         $paragraph .= "$s ";
  4994.                 $self->{install_failed}{$s} = undef;
  4995.                 $reported{$s} = undef;
  4996.               }
  4997.         }
  4998.             my $report_propagated;
  4999.             for $s (sort keys %{$self->{install_failed}}) {
  5000.               next if exists $reported{$s};
  5001.               $paragraph .= "and the following items had problems
  5002. during recursive bundle calls: " unless $report_propagated++;
  5003.               $paragraph .= "$s ";
  5004.             }
  5005.         $CPAN::Frontend->myprint(Text::Wrap::fill("  ","  ",$paragraph));
  5006.         $CPAN::Frontend->myprint("\n");
  5007.     } else {
  5008.         $self->{'install'} = 'YES';
  5009.     }
  5010.     }
  5011. }
  5012.  
  5013. #sub CPAN::Bundle::xs_file
  5014. sub xs_file {
  5015.     # If a bundle contains another that contains an xs_file we have
  5016.     # here, we just don't bother I suppose
  5017.     return 0;
  5018. }
  5019.  
  5020. #-> sub CPAN::Bundle::force ;
  5021. sub force   { shift->rematein('force',@_); }
  5022. #-> sub CPAN::Bundle::get ;
  5023. sub get     { shift->rematein('get',@_); }
  5024. #-> sub CPAN::Bundle::make ;
  5025. sub make    { shift->rematein('make',@_); }
  5026. #-> sub CPAN::Bundle::test ;
  5027. sub test    {
  5028.     my $self = shift;
  5029.     $self->{badtestcnt} ||= 0;
  5030.     $self->rematein('test',@_);
  5031. }
  5032. #-> sub CPAN::Bundle::install ;
  5033. sub install {
  5034.   my $self = shift;
  5035.   $self->rematein('install',@_);
  5036. }
  5037. #-> sub CPAN::Bundle::clean ;
  5038. sub clean   { shift->rematein('clean',@_); }
  5039.  
  5040. #-> sub CPAN::Bundle::uptodate ;
  5041. sub uptodate {
  5042.     my($self) = @_;
  5043.     return 0 unless $self->SUPER::uptodate; # we mut have the current Bundle def
  5044.     my $c;
  5045.     foreach $c ($self->contains) {
  5046.         my $obj = CPAN::Shell->expandany($c);
  5047.         return 0 unless $obj->uptodate;
  5048.     }
  5049.     return 1;
  5050. }
  5051.  
  5052. #-> sub CPAN::Bundle::readme ;
  5053. sub readme  {
  5054.     my($self) = @_;
  5055.     my($file) = $self->cpan_file or $CPAN::Frontend->myprint(qq{
  5056. No File found for bundle } . $self->id . qq{\n}), return;
  5057.     $self->debug("self[$self] file[$file]") if $CPAN::DEBUG;
  5058.     $CPAN::META->instance('CPAN::Distribution',$file)->readme;
  5059. }
  5060.  
  5061. package CPAN::Module;
  5062.  
  5063. # Accessors
  5064. # sub cpan_userid { shift->{RO}{CPAN_USERID} }
  5065. sub userid {
  5066.     my $self = shift;
  5067.     return unless exists $self->{RO}; # should never happen
  5068.     return $self->{RO}{CPAN_USERID} || $self->{RO}{userid};
  5069. }
  5070. sub description { shift->{RO}{description} }
  5071.  
  5072. sub undelay {
  5073.     my $self = shift;
  5074.     delete $self->{later};
  5075.     if ( my $dist = CPAN::Shell->expand("Distribution", $self->cpan_file) ) {
  5076.         $dist->undelay;
  5077.     }
  5078. }
  5079.  
  5080. #-> sub CPAN::Module::color_cmd_tmps ;
  5081. sub color_cmd_tmps {
  5082.     my($self) = shift;
  5083.     my($depth) = shift || 0;
  5084.     my($color) = shift || 0;
  5085.     # a module needs to recurse to its cpan_file
  5086.  
  5087.     return if exists $self->{incommandcolor}
  5088.         && $self->{incommandcolor}==$color;
  5089.     $CPAN::Frontend->mydie(sprintf("CPAN.pm panic: deep recursion in ".
  5090.                                    "color_cmd_tmps depth[%s] self[%s] id[%s]",
  5091.                                    $depth,
  5092.                                    $self,
  5093.                                    $self->id
  5094.                                   )) if $depth>=100;
  5095.     ##### warn "color_cmd_tmps $depth $color " . $self->id; # sleep 1;
  5096.  
  5097.     if ( my $dist = CPAN::Shell->expand("Distribution", $self->cpan_file) ) {
  5098.         $dist->color_cmd_tmps($depth+1,$color);
  5099.     }
  5100.     if ($color==0) {
  5101.         delete $self->{badtestcnt};
  5102.     }
  5103.     $self->{incommandcolor} = $color;
  5104. }
  5105.  
  5106. #-> sub CPAN::Module::as_glimpse ;
  5107. sub as_glimpse {
  5108.     my($self) = @_;
  5109.     my(@m);
  5110.     my $class = ref($self);
  5111.     $class =~ s/^CPAN:://;
  5112.     my $color_on = "";
  5113.     my $color_off = "";
  5114.     if (
  5115.         $CPAN::Shell::COLOR_REGISTERED
  5116.         &&
  5117.         $CPAN::META->has_inst("Term::ANSIColor")
  5118.         &&
  5119.         $self->{RO}{description}
  5120.        ) {
  5121.         $color_on = Term::ANSIColor::color("green");
  5122.         $color_off = Term::ANSIColor::color("reset");
  5123.     }
  5124.     push @m, sprintf("%-15s %s%-15s%s (%s)\n",
  5125.                      $class,
  5126.                      $color_on,
  5127.                      $self->id,
  5128.                      $color_off,
  5129.              $self->cpan_file);
  5130.     join "", @m;
  5131. }
  5132.  
  5133. #-> sub CPAN::Module::as_string ;
  5134. sub as_string {
  5135.     my($self) = @_;
  5136.     my(@m);
  5137.     CPAN->debug($self) if $CPAN::DEBUG;
  5138.     my $class = ref($self);
  5139.     $class =~ s/^CPAN:://;
  5140.     local($^W) = 0;
  5141.     push @m, $class, " id = $self->{ID}\n";
  5142.     my $sprintf = "    %-12s %s\n";
  5143.     push @m, sprintf($sprintf, 'DESCRIPTION', $self->description)
  5144.     if $self->description;
  5145.     my $sprintf2 = "    %-12s %s (%s)\n";
  5146.     my($userid);
  5147.     if ($userid = $self->cpan_userid || $self->userid){
  5148.     my $author;
  5149.     if ($author = CPAN::Shell->expand('Author',$userid)) {
  5150.       my $email = "";
  5151.       my $m; # old perls
  5152.       if ($m = $author->email) {
  5153.             $email = " <$m>";
  5154.           }
  5155.       push @m, sprintf(
  5156.                $sprintf2,
  5157.                'CPAN_USERID',
  5158.                $userid,
  5159.                $author->fullname . $email
  5160.               );
  5161.     }
  5162.     }
  5163.     push @m, sprintf($sprintf, 'CPAN_VERSION', $self->cpan_version)
  5164.     if $self->cpan_version;
  5165.     push @m, sprintf($sprintf, 'CPAN_FILE', $self->cpan_file)
  5166.     if $self->cpan_file;
  5167.     my $sprintf3 = "    %-12s %1s%1s%1s%1s (%s,%s,%s,%s)\n";
  5168.     my(%statd,%stats,%statl,%stati);
  5169.     @statd{qw,? i c a b R M S,} = qw,unknown idea
  5170.     pre-alpha alpha beta released mature standard,;
  5171.     @stats{qw,? m d u n,}       = qw,unknown mailing-list
  5172.     developer comp.lang.perl.* none,;
  5173.     @statl{qw,? p c + o h,}       = qw,unknown perl C C++ other hybrid,;
  5174.     @stati{qw,? f r O h,}         = qw,unknown functions
  5175.     references+ties object-oriented hybrid,;
  5176.     $statd{' '} = 'unknown';
  5177.     $stats{' '} = 'unknown';
  5178.     $statl{' '} = 'unknown';
  5179.     $stati{' '} = 'unknown';
  5180.     push @m, sprintf(
  5181.              $sprintf3,
  5182.              'DSLI_STATUS',
  5183.              $self->{RO}{statd},
  5184.              $self->{RO}{stats},
  5185.              $self->{RO}{statl},
  5186.              $self->{RO}{stati},
  5187.              $statd{$self->{RO}{statd}},
  5188.              $stats{$self->{RO}{stats}},
  5189.              $statl{$self->{RO}{statl}},
  5190.              $stati{$self->{RO}{stati}}
  5191.             ) if $self->{RO}{statd};
  5192.     my $local_file = $self->inst_file;
  5193.     unless ($self->{MANPAGE}) {
  5194.         if ($local_file) {
  5195.             $self->{MANPAGE} = $self->manpage_headline($local_file);
  5196.         } else {
  5197.             # If we have already untarred it, we should look there
  5198.             my $dist = $CPAN::META->instance('CPAN::Distribution',
  5199.                                              $self->cpan_file);
  5200.             # warn "dist[$dist]";
  5201.             # mff=manifest file; mfh=manifest handle
  5202.             my($mff,$mfh);
  5203.             if (
  5204.                 $dist->{build_dir}
  5205.                 and
  5206.                 (-f  ($mff = MM->catfile($dist->{build_dir}, "MANIFEST")))
  5207.                 and
  5208.                 $mfh = FileHandle->new($mff)
  5209.                ) {
  5210.                 CPAN->debug("mff[$mff]") if $CPAN::DEBUG;
  5211.                 my $lfre = $self->id; # local file RE
  5212.                 $lfre =~ s/::/./g;
  5213.                 $lfre .= "\\.pm\$";
  5214.                 my($lfl); # local file file
  5215.                 local $/ = "\n";
  5216.                 my(@mflines) = <$mfh>;
  5217.                 for (@mflines) {
  5218.                     s/^\s+//;
  5219.                     s/\s.*//s;
  5220.                 }
  5221.                 while (length($lfre)>5 and !$lfl) {
  5222.                     ($lfl) = grep /$lfre/, @mflines;
  5223.                     CPAN->debug("lfl[$lfl]lfre[$lfre]") if $CPAN::DEBUG;
  5224.                     $lfre =~ s/.+?\.//;
  5225.                 }
  5226.                 $lfl =~ s/\s.*//; # remove comments
  5227.                 $lfl =~ s/\s+//g; # chomp would maybe be too system-specific
  5228.                 my $lfl_abs = MM->catfile($dist->{build_dir},$lfl);
  5229.                 # warn "lfl_abs[$lfl_abs]";
  5230.                 if (-f $lfl_abs) {
  5231.                     $self->{MANPAGE} = $self->manpage_headline($lfl_abs);
  5232.                 }
  5233.             }
  5234.         }
  5235.     }
  5236.     my($item);
  5237.     for $item (qw/MANPAGE/) {
  5238.     push @m, sprintf($sprintf, $item, $self->{$item})
  5239.         if exists $self->{$item};
  5240.     }
  5241.     for $item (qw/CONTAINS/) {
  5242.     push @m, sprintf($sprintf, $item, join(" ",@{$self->{$item}}))
  5243.         if exists $self->{$item} && @{$self->{$item}};
  5244.     }
  5245.     push @m, sprintf($sprintf, 'INST_FILE',
  5246.              $local_file || "(not installed)");
  5247.     push @m, sprintf($sprintf, 'INST_VERSION',
  5248.              $self->inst_version) if $local_file;
  5249.     join "", @m, "\n";
  5250. }
  5251.  
  5252. sub manpage_headline {
  5253.   my($self,$local_file) = @_;
  5254.   my(@local_file) = $local_file;
  5255.   $local_file =~ s/\.pm(?!\n)\Z/.pod/;
  5256.   push @local_file, $local_file;
  5257.   my(@result,$locf);
  5258.   for $locf (@local_file) {
  5259.     next unless -f $locf;
  5260.     my $fh = FileHandle->new($locf)
  5261.     or $Carp::Frontend->mydie("Couldn't open $locf: $!");
  5262.     my $inpod = 0;
  5263.     local $/ = "\n";
  5264.     while (<$fh>) {
  5265.       $inpod = m/^=(?!head1\s+NAME)/ ? 0 :
  5266.       m/^=head1\s+NAME/ ? 1 : $inpod;
  5267.       next unless $inpod;
  5268.       next if /^=/;
  5269.       next if /^\s+$/;
  5270.       chomp;
  5271.       push @result, $_;
  5272.     }
  5273.     close $fh;
  5274.     last if @result;
  5275.   }
  5276.   join " ", @result;
  5277. }
  5278.  
  5279. #-> sub CPAN::Module::cpan_file ;
  5280. # Note: also inherited by CPAN::Bundle
  5281. sub cpan_file {
  5282.     my $self = shift;
  5283.     CPAN->debug(sprintf "id[%s]", $self->id) if $CPAN::DEBUG;
  5284.     unless (defined $self->{RO}{CPAN_FILE}) {
  5285.     CPAN::Index->reload;
  5286.     }
  5287.     if (exists $self->{RO}{CPAN_FILE} && defined $self->{RO}{CPAN_FILE}){
  5288.     return $self->{RO}{CPAN_FILE};
  5289.     } else {
  5290.         my $userid = $self->userid;
  5291.         if ( $userid ) {
  5292.             if ($CPAN::META->exists("CPAN::Author",$userid)) {
  5293.                 my $author = $CPAN::META->instance("CPAN::Author",
  5294.                                                    $userid);
  5295.                 my $fullname = $author->fullname;
  5296.                 my $email = $author->email;
  5297.                 unless (defined $fullname && defined $email) {
  5298.                     return sprintf("Contact Author %s",
  5299.                                    $userid,
  5300.                                   );
  5301.                 }
  5302.                 return "Contact Author $fullname <$email>";
  5303.             } else {
  5304.                 return "UserID $userid";
  5305.             }
  5306.         } else {
  5307.             return "N/A";
  5308.         }
  5309.     }
  5310. }
  5311.  
  5312. #-> sub CPAN::Module::cpan_version ;
  5313. sub cpan_version {
  5314.     my $self = shift;
  5315.  
  5316.     $self->{RO}{CPAN_VERSION} = 'undef'
  5317.     unless defined $self->{RO}{CPAN_VERSION};
  5318.     # I believe this is always a bug in the index and should be reported
  5319.     # as such, but usually I find out such an error and do not want to
  5320.     # provoke too many bugreports
  5321.  
  5322.     $self->{RO}{CPAN_VERSION};
  5323. }
  5324.  
  5325. #-> sub CPAN::Module::force ;
  5326. sub force {
  5327.     my($self) = @_;
  5328.     $self->{'force_update'}++;
  5329. }
  5330.  
  5331. #-> sub CPAN::Module::rematein ;
  5332. sub rematein {
  5333.     my($self,$meth) = @_;
  5334.     $CPAN::Frontend->myprint(sprintf("Running %s for module %s\n",
  5335.                                      $meth,
  5336.                                      $self->id));
  5337.     my $cpan_file = $self->cpan_file;
  5338.     if ($cpan_file eq "N/A" || $cpan_file =~ /^Contact Author/){
  5339.       $CPAN::Frontend->mywarn(sprintf qq{
  5340.   The module %s isn\'t available on CPAN.
  5341.  
  5342.   Either the module has not yet been uploaded to CPAN, or it is
  5343.   temporary unavailable. Please contact the author to find out
  5344.   more about the status. Try 'i %s'.
  5345. },
  5346.                   $self->id,
  5347.                   $self->id,
  5348.                  );
  5349.       return;
  5350.     }
  5351.     my $pack = $CPAN::META->instance('CPAN::Distribution',$cpan_file);
  5352.     $pack->called_for($self->id);
  5353.     $pack->force($meth) if exists $self->{'force_update'};
  5354.     $pack->$meth();
  5355.     $pack->unforce if $pack->can("unforce") && exists $self->{'force_update'};
  5356.     delete $self->{'force_update'};
  5357. }
  5358.  
  5359. #-> sub CPAN::Module::readme ;
  5360. sub readme { shift->rematein('readme') }
  5361. #-> sub CPAN::Module::look ;
  5362. sub look { shift->rematein('look') }
  5363. #-> sub CPAN::Module::cvs_import ;
  5364. sub cvs_import { shift->rematein('cvs_import') }
  5365. #-> sub CPAN::Module::get ;
  5366. sub get    { shift->rematein('get',@_); }
  5367. #-> sub CPAN::Module::make ;
  5368. sub make   {
  5369.     my $self = shift;
  5370.     $self->rematein('make');
  5371. }
  5372. #-> sub CPAN::Module::test ;
  5373. sub test   {
  5374.     my $self = shift;
  5375.     $self->{badtestcnt} ||= 0;
  5376.     $self->rematein('test',@_);
  5377. }
  5378. #-> sub CPAN::Module::uptodate ;
  5379. sub uptodate {
  5380.     my($self) = @_;
  5381.     my($latest) = $self->cpan_version;
  5382.     $latest ||= 0;
  5383.     my($inst_file) = $self->inst_file;
  5384.     my($have) = 0;
  5385.     if (defined $inst_file) {
  5386.     $have = $self->inst_version;
  5387.     }
  5388.     local($^W)=0;
  5389.     if ($inst_file
  5390.     &&
  5391.     ! CPAN::Version->vgt($latest, $have)
  5392.        ) {
  5393.         CPAN->debug("returning uptodate. inst_file[$inst_file] ".
  5394.                     "latest[$latest] have[$have]") if $CPAN::DEBUG;
  5395.         return 1;
  5396.     }
  5397.     return;
  5398. }
  5399. #-> sub CPAN::Module::install ;
  5400. sub install {
  5401.     my($self) = @_;
  5402.     my($doit) = 0;
  5403.     if ($self->uptodate
  5404.     &&
  5405.     not exists $self->{'force_update'}
  5406.        ) {
  5407.     $CPAN::Frontend->myprint( $self->id. " is up to date.\n");
  5408.     } else {
  5409.     $doit = 1;
  5410.     }
  5411.     $self->rematein('install') if $doit;
  5412. }
  5413. #-> sub CPAN::Module::clean ;
  5414. sub clean  { shift->rematein('clean') }
  5415.  
  5416. #-> sub CPAN::Module::inst_file ;
  5417. sub inst_file {
  5418.     my($self) = @_;
  5419.     my($dir,@packpath);
  5420.     @packpath = split /::/, $self->{ID};
  5421.     $packpath[-1] .= ".pm";
  5422.     foreach $dir (@INC) {
  5423.     my $pmfile = MM->catfile($dir,@packpath);
  5424.     if (-f $pmfile){
  5425.         return $pmfile;
  5426.     }
  5427.     }
  5428.     return;
  5429. }
  5430.  
  5431. #-> sub CPAN::Module::xs_file ;
  5432. sub xs_file {
  5433.     my($self) = @_;
  5434.     my($dir,@packpath);
  5435.     @packpath = split /::/, $self->{ID};
  5436.     push @packpath, $packpath[-1];
  5437.     $packpath[-1] .= "." . $Config::Config{'dlext'};
  5438.     foreach $dir (@INC) {
  5439.     my $xsfile = MM->catfile($dir,'auto',@packpath);
  5440.     if (-f $xsfile){
  5441.         return $xsfile;
  5442.     }
  5443.     }
  5444.     return;
  5445. }
  5446.  
  5447. #-> sub CPAN::Module::inst_version ;
  5448. sub inst_version {
  5449.     my($self) = @_;
  5450.     my $parsefile = $self->inst_file or return;
  5451.     local($^W) = 0 if $] < 5.00303 && $ExtUtils::MakeMaker::VERSION < 5.38;
  5452.     my $have;
  5453.  
  5454.     # there was a bug in 5.6.0 that let lots of unini warnings out of
  5455.     # parse_version. Fixed shortly after 5.6.0 by PMQS. We can remove
  5456.     # the following workaround after 5.6.1 is out.
  5457.     local($SIG{__WARN__}) =  sub { my $w = shift;
  5458.                                    return if $w =~ /uninitialized/i;
  5459.                                    warn $w;
  5460.                                  };
  5461.  
  5462.     $have = MM->parse_version($parsefile) || "undef";
  5463.     $have =~ s/^ //; # since the %vd hack these two lines here are needed
  5464.     $have =~ s/ $//; # trailing whitespace happens all the time
  5465.  
  5466.     # My thoughts about why %vd processing should happen here
  5467.  
  5468.     # Alt1 maintain it as string with leading v:
  5469.     # read index files     do nothing
  5470.     # compare it           use utility for compare
  5471.     # print it             do nothing
  5472.  
  5473.     # Alt2 maintain it as what is is
  5474.     # read index files     convert
  5475.     # compare it           use utility because there's still a ">" vs "gt" issue
  5476.     # print it             use CPAN::Version for print
  5477.  
  5478.     # Seems cleaner to hold it in memory as a string starting with a "v"
  5479.  
  5480.     # If the author of this module made a mistake and wrote a quoted
  5481.     # "v1.13" instead of v1.13, we simply leave it at that with the
  5482.     # effect that *we* will treat it like a v-tring while the rest of
  5483.     # perl won't. Seems sensible when we consider that any action we
  5484.     # could take now would just add complexity.
  5485.  
  5486.     $have = CPAN::Version->readable($have);
  5487.  
  5488.     $have =~ s/\s*//g; # stringify to float around floating point issues
  5489.     $have; # no stringify needed, \s* above matches always
  5490. }
  5491.  
  5492. package CPAN::Tarzip;
  5493.  
  5494. # CPAN::Tarzip::gzip
  5495. sub gzip {
  5496.   my($class,$read,$write) = @_;
  5497.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5498.     my($buffer,$fhw);
  5499.     $fhw = FileHandle->new($read)
  5500.     or $CPAN::Frontend->mydie("Could not open $read: $!");
  5501.     my $gz = Compress::Zlib::gzopen($write, "wb")
  5502.     or $CPAN::Frontend->mydie("Cannot gzopen $write: $!\n");
  5503.     $gz->gzwrite($buffer)
  5504.     while read($fhw,$buffer,4096) > 0 ;
  5505.     $gz->gzclose() ;
  5506.     $fhw->close;
  5507.     return 1;
  5508.   } else {
  5509.     system("$CPAN::Config->{gzip} -c $read > $write")==0;
  5510.   }
  5511. }
  5512.  
  5513.  
  5514. # CPAN::Tarzip::gunzip
  5515. sub gunzip {
  5516.   my($class,$read,$write) = @_;
  5517.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5518.     my($buffer,$fhw);
  5519.     $fhw = FileHandle->new(">$write")
  5520.     or $CPAN::Frontend->mydie("Could not open >$write: $!");
  5521.     my $gz = Compress::Zlib::gzopen($read, "rb")
  5522.     or $CPAN::Frontend->mydie("Cannot gzopen $read: $!\n");
  5523.     $fhw->print($buffer)
  5524.     while $gz->gzread($buffer) > 0 ;
  5525.     $CPAN::Frontend->mydie("Error reading from $read: $!\n")
  5526.     if $gz->gzerror != Compress::Zlib::Z_STREAM_END();
  5527.     $gz->gzclose() ;
  5528.     $fhw->close;
  5529.     return 1;
  5530.   } else {
  5531.     system("$CPAN::Config->{gzip} -dc $read > $write")==0;
  5532.   }
  5533. }
  5534.  
  5535.  
  5536. # CPAN::Tarzip::gtest
  5537. sub gtest {
  5538.   my($class,$read) = @_;
  5539.   # After I had reread the documentation in zlib.h, I discovered that
  5540.   # uncompressed files do not lead to an gzerror (anymore?).
  5541.   if ( $CPAN::META->has_inst("Compress::Zlib") ) {
  5542.     my($buffer,$len);
  5543.     $len = 0;
  5544.     my $gz = Compress::Zlib::gzopen($read, "rb")
  5545.     or $CPAN::Frontend->mydie(sprintf("Cannot gzopen %s: %s\n",
  5546.                                           $read,
  5547.                                           $Compress::Zlib::gzerrno));
  5548.     while ($gz->gzread($buffer) > 0 ){
  5549.         $len += length($buffer);
  5550.         $buffer = "";
  5551.     }
  5552.     my $err = $gz->gzerror;
  5553.     my $success = ! $err || $err == Compress::Zlib::Z_STREAM_END();
  5554.     if ($len == -s $read){
  5555.         $success = 0;
  5556.         CPAN->debug("hit an uncompressed file") if $CPAN::DEBUG;
  5557.     }
  5558.     $gz->gzclose();
  5559.     CPAN->debug("err[$err]success[$success]") if $CPAN::DEBUG;
  5560.     return $success;
  5561.   } else {
  5562.       return system("$CPAN::Config->{gzip} -dt $read")==0;
  5563.   }
  5564. }
  5565.  
  5566.  
  5567. # CPAN::Tarzip::TIEHANDLE
  5568. sub TIEHANDLE {
  5569.   my($class,$file) = @_;
  5570.   my $ret;
  5571.   $class->debug("file[$file]");
  5572.   if ($CPAN::META->has_inst("Compress::Zlib")) {
  5573.     my $gz = Compress::Zlib::gzopen($file,"rb") or
  5574.     die "Could not gzopen $file";
  5575.     $ret = bless {GZ => $gz}, $class;
  5576.   } else {
  5577.     my $pipe = "$CPAN::Config->{gzip} --decompress --stdout $file |";
  5578.     my $fh = FileHandle->new($pipe) or die "Could not pipe[$pipe]: $!";
  5579.     binmode $fh;
  5580.     $ret = bless {FH => $fh}, $class;
  5581.   }
  5582.   $ret;
  5583. }
  5584.  
  5585.  
  5586. # CPAN::Tarzip::READLINE
  5587. sub READLINE {
  5588.   my($self) = @_;
  5589.   if (exists $self->{GZ}) {
  5590.     my $gz = $self->{GZ};
  5591.     my($line,$bytesread);
  5592.     $bytesread = $gz->gzreadline($line);
  5593.     return undef if $bytesread <= 0;
  5594.     return $line;
  5595.   } else {
  5596.     my $fh = $self->{FH};
  5597.     return scalar <$fh>;
  5598.   }
  5599. }
  5600.  
  5601.  
  5602. # CPAN::Tarzip::READ
  5603. sub READ {
  5604.   my($self,$ref,$length,$offset) = @_;
  5605.   die "read with offset not implemented" if defined $offset;
  5606.   if (exists $self->{GZ}) {
  5607.     my $gz = $self->{GZ};
  5608.     my $byteread = $gz->gzread($$ref,$length);# 30eaf79e8b446ef52464b5422da328a8
  5609.     return $byteread;
  5610.   } else {
  5611.     my $fh = $self->{FH};
  5612.     return read($fh,$$ref,$length);
  5613.   }
  5614. }
  5615.  
  5616.  
  5617. # CPAN::Tarzip::DESTROY
  5618. sub DESTROY {
  5619.     my($self) = @_;
  5620.     if (exists $self->{GZ}) {
  5621.         my $gz = $self->{GZ};
  5622.         $gz->gzclose() if defined $gz; # hard to say if it is allowed
  5623.                                        # to be undef ever. AK, 2000-09
  5624.     } else {
  5625.         my $fh = $self->{FH};
  5626.         $fh->close if defined $fh;
  5627.     }
  5628.     undef $self;
  5629. }
  5630.  
  5631.  
  5632. # CPAN::Tarzip::untar
  5633. sub untar {
  5634.   my($class,$file) = @_;
  5635.   my($prefer) = 0;
  5636.  
  5637.   if (0) { # makes changing order easier
  5638.   } elsif ($BUGHUNTING){
  5639.       $prefer=2;
  5640.   } elsif (MM->maybe_command($CPAN::Config->{gzip})
  5641.            &&
  5642.            MM->maybe_command($CPAN::Config->{'tar'})) {
  5643.       # should be default until Archive::Tar is fixed
  5644.       $prefer = 1;
  5645.   } elsif (
  5646.            $CPAN::META->has_inst("Archive::Tar")
  5647.            &&
  5648.            $CPAN::META->has_inst("Compress::Zlib") ) {
  5649.       $prefer = 2;
  5650.   } else {
  5651.     $CPAN::Frontend->mydie(qq{
  5652. CPAN.pm needs either both external programs tar and gzip installed or
  5653. both the modules Archive::Tar and Compress::Zlib. Neither prerequisite
  5654. is available. Can\'t continue.
  5655. });
  5656.   }
  5657.   if ($prefer==1) { # 1 => external gzip+tar
  5658.     my($system);
  5659.     my $is_compressed = $class->gtest($file);
  5660.     if ($is_compressed) {
  5661.         $system = "$CPAN::Config->{gzip} --decompress --stdout " .
  5662.             "< $file | $CPAN::Config->{tar} xvf -";
  5663.     } else {
  5664.         $system = "$CPAN::Config->{tar} xvf $file";
  5665.     }
  5666.     if (system($system) != 0) {
  5667.         # people find the most curious tar binaries that cannot handle
  5668.         # pipes
  5669.         if ($is_compressed) {
  5670.             (my $ungzf = $file) =~ s/\.gz(?!\n)\Z//;
  5671.             if (CPAN::Tarzip->gunzip($file, $ungzf)) {
  5672.                 $CPAN::Frontend->myprint(qq{Uncompressed $file successfully\n});
  5673.             } else {
  5674.                 $CPAN::Frontend->mydie(qq{Couldn\'t uncompress $file\n});
  5675.             }
  5676.             $file = $ungzf;
  5677.         }
  5678.         $system = "$CPAN::Config->{tar} xvf $file";
  5679.         $CPAN::Frontend->myprint(qq{Using Tar:$system:\n});
  5680.         if (system($system)==0) {
  5681.             $CPAN::Frontend->myprint(qq{Untarred $file successfully\n});
  5682.         } else {
  5683.             $CPAN::Frontend->mydie(qq{Couldn\'t untar $file\n});
  5684.         }
  5685.         return 1;
  5686.     } else {
  5687.         return 1;
  5688.     }
  5689.   } elsif ($prefer==2) { # 2 => modules
  5690.     my $tar = Archive::Tar->new($file,1);
  5691.     my $af; # archive file
  5692.     my @af;
  5693.     if ($BUGHUNTING) {
  5694.         # RCS 1.337 had this code, it turned out unacceptable slow but
  5695.         # it revealed a bug in Archive::Tar. Code is only here to hunt
  5696.         # the bug again. It should never be enabled in published code.
  5697.         # GDGraph3d-0.53 was an interesting case according to Larry
  5698.         # Virden.
  5699.         warn(">>>Bughunting code enabled<<< " x 20);
  5700.         for $af ($tar->list_files) {
  5701.             if ($af =~ m!^(/|\.\./)!) {
  5702.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5703.                                        "illegal member [$af]");
  5704.             }
  5705.             $CPAN::Frontend->myprint("$af\n");
  5706.             $tar->extract($af); # slow but effective for finding the bug
  5707.             return if $CPAN::Signal;
  5708.         }
  5709.     } else {
  5710.         for $af ($tar->list_files) {
  5711.             if ($af =~ m!^(/|\.\./)!) {
  5712.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5713.                                        "illegal member [$af]");
  5714.             }
  5715.             $CPAN::Frontend->myprint("$af\n");
  5716.             push @af, $af;
  5717.             return if $CPAN::Signal;
  5718.         }
  5719.         $tar->extract(@af);
  5720.     }
  5721.  
  5722.     ExtUtils::MM_MacOS::convert_files([$tar->list_files], 1)
  5723.         if ($^O eq 'MacOS');
  5724.  
  5725.     return 1;
  5726.   }
  5727. }
  5728.  
  5729. sub unzip {
  5730.     my($class,$file) = @_;
  5731.     if ($CPAN::META->has_inst("Archive::Zip")) {
  5732.         # blueprint of the code from Archive::Zip::Tree::extractTree();
  5733.         my $zip = Archive::Zip->new();
  5734.         my $status;
  5735.         $status = $zip->read($file);
  5736.         die "Read of file[$file] failed\n" if $status != Archive::Zip::AZ_OK();
  5737.         $CPAN::META->debug("Successfully read file[$file]") if $CPAN::DEBUG;
  5738.         my @members = $zip->members();
  5739.         for my $member ( @members ) {
  5740.             my $af = $member->fileName();
  5741.             if ($af =~ m!^(/|\.\./)!) {
  5742.                 $CPAN::Frontend->mydie("ALERT: Archive contains ".
  5743.                                        "illegal member [$af]");
  5744.             }
  5745.             my $status = $member->extractToFileNamed( $af );
  5746.             $CPAN::META->debug("af[$af]status[$status]") if $CPAN::DEBUG;
  5747.             die "Extracting of file[$af] from zipfile[$file] failed\n" if
  5748.                 $status != Archive::Zip::AZ_OK();
  5749.             return if $CPAN::Signal;
  5750.         }
  5751.         return 1;
  5752.     } else {
  5753.         my $unzip = $CPAN::Config->{unzip} or
  5754.             $CPAN::Frontend->mydie("Cannot unzip, no unzip program available");
  5755.         my @system = ($unzip, $file);
  5756.         return system(@system) == 0;
  5757.     }
  5758. }
  5759.  
  5760.  
  5761. package CPAN::Version;
  5762. # CPAN::Version::vcmp courtesy Jost Krieger
  5763. sub vcmp {
  5764.   my($self,$l,$r) = @_;
  5765.   local($^W) = 0;
  5766.   CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG;
  5767.  
  5768.   return 0 if $l eq $r; # short circuit for quicker success
  5769.  
  5770.   if ($l=~/^v/ <=> $r=~/^v/) {
  5771.       for ($l,$r) {
  5772.           next if /^v/;
  5773.           $_ = $self->float2vv($_);
  5774.       }
  5775.   }
  5776.  
  5777.   return
  5778.       ($l ne "undef") <=> ($r ne "undef") ||
  5779.           ($] >= 5.006 &&
  5780.            $l =~ /^v/ &&
  5781.            $r =~ /^v/ &&
  5782.            $self->vstring($l) cmp $self->vstring($r)) ||
  5783.                $l <=> $r ||
  5784.                    $l cmp $r;
  5785. }
  5786.  
  5787. sub vgt {
  5788.   my($self,$l,$r) = @_;
  5789.   $self->vcmp($l,$r) > 0;
  5790. }
  5791.  
  5792. sub vstring {
  5793.   my($self,$n) = @_;
  5794.   $n =~ s/^v// or die "CPAN::Version::vstring() called with invalid arg [$n]";
  5795.   pack "U*", split /\./, $n;
  5796. }
  5797.  
  5798. # vv => visible vstring
  5799. sub float2vv {
  5800.     my($self,$n) = @_;
  5801.     my($rev) = int($n);
  5802.     $rev ||= 0;
  5803.     my($mantissa) = $n =~ /\.(\d{1,12})/; # limit to 12 digits to limit
  5804.                                           # architecture influence
  5805.     $mantissa ||= 0;
  5806.     $mantissa .= "0" while length($mantissa)%3;
  5807.     my $ret = "v" . $rev;
  5808.     while ($mantissa) {
  5809.         $mantissa =~ s/(\d{1,3})// or
  5810.             die "Panic: length>0 but not a digit? mantissa[$mantissa]";
  5811.         $ret .= ".".int($1);
  5812.     }
  5813.     # warn "n[$n]ret[$ret]";
  5814.     $ret;
  5815. }
  5816.  
  5817. sub readable {
  5818.   my($self,$n) = @_;
  5819.   $n =~ /^([\w\-\+\.]+)/;
  5820.  
  5821.   return $1 if defined $1 && length($1)>0;
  5822.   # if the first user reaches version v43, he will be treated as "+".
  5823.   # We'll have to decide about a new rule here then, depending on what
  5824.   # will be the prevailing versioning behavior then.
  5825.  
  5826.   if ($] < 5.006) { # or whenever v-strings were introduced
  5827.     # we get them wrong anyway, whatever we do, because 5.005 will
  5828.     # have already interpreted 0.2.4 to be "0.24". So even if he
  5829.     # indexer sends us something like "v0.2.4" we compare wrongly.
  5830.  
  5831.     # And if they say v1.2, then the old perl takes it as "v12"
  5832.  
  5833.     $CPAN::Frontend->mywarn("Suspicious version string seen [$n]");
  5834.     return $n;
  5835.   }
  5836.   my $better = sprintf "v%vd", $n;
  5837.   CPAN->debug("n[$n] better[$better]") if $CPAN::DEBUG;
  5838.   return $better;
  5839. }
  5840.  
  5841. package CPAN;
  5842.  
  5843. 1;
  5844.  
  5845. __END__
  5846.  
  5847. =head1 NAME
  5848.  
  5849. CPAN - query, download and build perl modules from CPAN sites
  5850.  
  5851. =head1 SYNOPSIS
  5852.  
  5853. Interactive mode:
  5854.  
  5855.   perl -MCPAN -e shell;
  5856.  
  5857. Batch mode:
  5858.  
  5859.   use CPAN;
  5860.  
  5861.   autobundle, clean, install, make, recompile, test
  5862.  
  5863. =head1 DESCRIPTION
  5864.  
  5865. The CPAN module is designed to automate the make and install of perl
  5866. modules and extensions. It includes some searching capabilities and
  5867. knows how to use Net::FTP or LWP (or lynx or an external ftp client)
  5868. to fetch the raw data from the net.
  5869.  
  5870. Modules are fetched from one or more of the mirrored CPAN
  5871. (Comprehensive Perl Archive Network) sites and unpacked in a dedicated
  5872. directory.
  5873.  
  5874. The CPAN module also supports the concept of named and versioned
  5875. I<bundles> of modules. Bundles simplify the handling of sets of
  5876. related modules. See Bundles below.
  5877.  
  5878. The package contains a session manager and a cache manager. There is
  5879. no status retained between sessions. The session manager keeps track
  5880. of what has been fetched, built and installed in the current
  5881. session. The cache manager keeps track of the disk space occupied by
  5882. the make processes and deletes excess space according to a simple FIFO
  5883. mechanism.
  5884.  
  5885. For extended searching capabilities there's a plugin for CPAN available,
  5886. L<C<CPAN::WAIT>|CPAN::WAIT>. C<CPAN::WAIT> is a full-text search engine
  5887. that indexes all documents available in CPAN authors directories. If
  5888. C<CPAN::WAIT> is installed on your system, the interactive shell of
  5889. CPAN.pm will enable the C<wq>, C<wr>, C<wd>, C<wl>, and C<wh> commands
  5890. which send queries to the WAIT server that has been configured for your
  5891. installation.
  5892.  
  5893. All other methods provided are accessible in a programmer style and in an
  5894. interactive shell style.
  5895.  
  5896. =head2 Interactive Mode
  5897.  
  5898. The interactive mode is entered by running
  5899.  
  5900.     perl -MCPAN -e shell
  5901.  
  5902. which puts you into a readline interface. You will have the most fun if
  5903. you install Term::ReadKey and Term::ReadLine to enjoy both history and
  5904. command completion.
  5905.  
  5906. Once you are on the command line, type 'h' and the rest should be
  5907. self-explanatory.
  5908.  
  5909. The function call C<shell> takes two optional arguments, one is the
  5910. prompt, the second is the default initial command line (the latter
  5911. only works if a real ReadLine interface module is installed).
  5912.  
  5913. The most common uses of the interactive modes are
  5914.  
  5915. =over 2
  5916.  
  5917. =item Searching for authors, bundles, distribution files and modules
  5918.  
  5919. There are corresponding one-letter commands C<a>, C<b>, C<d>, and C<m>
  5920. for each of the four categories and another, C<i> for any of the
  5921. mentioned four. Each of the four entities is implemented as a class
  5922. with slightly differing methods for displaying an object.
  5923.  
  5924. Arguments you pass to these commands are either strings exactly matching
  5925. the identification string of an object or regular expressions that are
  5926. then matched case-insensitively against various attributes of the
  5927. objects. The parser recognizes a regular expression only if you
  5928. enclose it between two slashes.
  5929.  
  5930. The principle is that the number of found objects influences how an
  5931. item is displayed. If the search finds one item, the result is
  5932. displayed with the rather verbose method C<as_string>, but if we find
  5933. more than one, we display each object with the terse method
  5934. <as_glimpse>.
  5935.  
  5936. =item make, test, install, clean  modules or distributions
  5937.  
  5938. These commands take any number of arguments and investigate what is
  5939. necessary to perform the action. If the argument is a distribution
  5940. file name (recognized by embedded slashes), it is processed. If it is
  5941. a module, CPAN determines the distribution file in which this module
  5942. is included and processes that, following any dependencies named in
  5943. the module's Makefile.PL (this behavior is controlled by
  5944. I<prerequisites_policy>.)
  5945.  
  5946. Any C<make> or C<test> are run unconditionally. An
  5947.  
  5948.   install <distribution_file>
  5949.  
  5950. also is run unconditionally. But for
  5951.  
  5952.   install <module>
  5953.  
  5954. CPAN checks if an install is actually needed for it and prints
  5955. I<module up to date> in the case that the distribution file containing
  5956. the module doesn't need to be updated.
  5957.  
  5958. CPAN also keeps track of what it has done within the current session
  5959. and doesn't try to build a package a second time regardless if it
  5960. succeeded or not. The C<force> command takes as a first argument the
  5961. method to invoke (currently: C<make>, C<test>, or C<install>) and executes the
  5962. command from scratch.
  5963.  
  5964. Example:
  5965.  
  5966.     cpan> install OpenGL
  5967.     OpenGL is up to date.
  5968.     cpan> force install OpenGL
  5969.     Running make
  5970.     OpenGL-0.4/
  5971.     OpenGL-0.4/COPYRIGHT
  5972.     [...]
  5973.  
  5974. A C<clean> command results in a
  5975.  
  5976.   make clean
  5977.  
  5978. being executed within the distribution file's working directory.
  5979.  
  5980. =item get, readme, look module or distribution
  5981.  
  5982. C<get> downloads a distribution file without further action. C<readme>
  5983. displays the README file of the associated distribution. C<Look> gets
  5984. and untars (if not yet done) the distribution file, changes to the
  5985. appropriate directory and opens a subshell process in that directory.
  5986.  
  5987. =item ls author
  5988.  
  5989. C<ls> lists all distribution files in and below an author's CPAN
  5990. directory. Only those files that contain modules are listed and if
  5991. there is more than one for any given module, only the most recent one
  5992. is listed.
  5993.  
  5994. =item Signals
  5995.  
  5996. CPAN.pm installs signal handlers for SIGINT and SIGTERM. While you are
  5997. in the cpan-shell it is intended that you can press C<^C> anytime and
  5998. return to the cpan-shell prompt. A SIGTERM will cause the cpan-shell
  5999. to clean up and leave the shell loop. You can emulate the effect of a
  6000. SIGTERM by sending two consecutive SIGINTs, which usually means by
  6001. pressing C<^C> twice.
  6002.  
  6003. CPAN.pm ignores a SIGPIPE. If the user sets inactivity_timeout, a
  6004. SIGALRM is used during the run of the C<perl Makefile.PL> subprocess.
  6005.  
  6006. =back
  6007.  
  6008. =head2 CPAN::Shell
  6009.  
  6010. The commands that are available in the shell interface are methods in
  6011. the package CPAN::Shell. If you enter the shell command, all your
  6012. input is split by the Text::ParseWords::shellwords() routine which
  6013. acts like most shells do. The first word is being interpreted as the
  6014. method to be called and the rest of the words are treated as arguments
  6015. to this method. Continuation lines are supported if a line ends with a
  6016. literal backslash.
  6017.  
  6018. =head2 autobundle
  6019.  
  6020. C<autobundle> writes a bundle file into the
  6021. C<$CPAN::Config-E<gt>{cpan_home}/Bundle> directory. The file contains
  6022. a list of all modules that are both available from CPAN and currently
  6023. installed within @INC. The name of the bundle file is based on the
  6024. current date and a counter.
  6025.  
  6026. =head2 recompile
  6027.  
  6028. recompile() is a very special command in that it takes no argument and
  6029. runs the make/test/install cycle with brute force over all installed
  6030. dynamically loadable extensions (aka XS modules) with 'force' in
  6031. effect. The primary purpose of this command is to finish a network
  6032. installation. Imagine, you have a common source tree for two different
  6033. architectures. You decide to do a completely independent fresh
  6034. installation. You start on one architecture with the help of a Bundle
  6035. file produced earlier. CPAN installs the whole Bundle for you, but
  6036. when you try to repeat the job on the second architecture, CPAN
  6037. responds with a C<"Foo up to date"> message for all modules. So you
  6038. invoke CPAN's recompile on the second architecture and you're done.
  6039.  
  6040. Another popular use for C<recompile> is to act as a rescue in case your
  6041. perl breaks binary compatibility. If one of the modules that CPAN uses
  6042. is in turn depending on binary compatibility (so you cannot run CPAN
  6043. commands), then you should try the CPAN::Nox module for recovery.
  6044.  
  6045. =head2 The four C<CPAN::*> Classes: Author, Bundle, Module, Distribution
  6046.  
  6047. Although it may be considered internal, the class hierarchy does matter
  6048. for both users and programmer. CPAN.pm deals with above mentioned four
  6049. classes, and all those classes share a set of methods. A classical
  6050. single polymorphism is in effect. A metaclass object registers all
  6051. objects of all kinds and indexes them with a string. The strings
  6052. referencing objects have a separated namespace (well, not completely
  6053. separated):
  6054.  
  6055.          Namespace                         Class
  6056.  
  6057.    words containing a "/" (slash)      Distribution
  6058.     words starting with Bundle::          Bundle
  6059.           everything else            Module or Author
  6060.  
  6061. Modules know their associated Distribution objects. They always refer
  6062. to the most recent official release. Developers may mark their releases
  6063. as unstable development versions (by inserting an underbar into the
  6064. visible version number), so the really hottest and newest distribution
  6065. file is not always the default.  If a module Foo circulates on CPAN in
  6066. both version 1.23 and 1.23_90, CPAN.pm offers a convenient way to
  6067. install version 1.23 by saying
  6068.  
  6069.     install Foo
  6070.  
  6071. This would install the complete distribution file (say
  6072. BAR/Foo-1.23.tar.gz) with all accompanying material. But if you would
  6073. like to install version 1.23_90, you need to know where the
  6074. distribution file resides on CPAN relative to the authors/id/
  6075. directory. If the author is BAR, this might be BAR/Foo-1.23_90.tar.gz;
  6076. so you would have to say
  6077.  
  6078.     install BAR/Foo-1.23_90.tar.gz
  6079.  
  6080. The first example will be driven by an object of the class
  6081. CPAN::Module, the second by an object of class CPAN::Distribution.
  6082.  
  6083. =head2 Programmer's interface
  6084.  
  6085. If you do not enter the shell, the available shell commands are both
  6086. available as methods (C<CPAN::Shell-E<gt>install(...)>) and as
  6087. functions in the calling package (C<install(...)>).
  6088.  
  6089. There's currently only one class that has a stable interface -
  6090. CPAN::Shell. All commands that are available in the CPAN shell are
  6091. methods of the class CPAN::Shell. Each of the commands that produce
  6092. listings of modules (C<r>, C<autobundle>, C<u>) also return a list of
  6093. the IDs of all modules within the list.
  6094.  
  6095. =over 2
  6096.  
  6097. =item expand($type,@things)
  6098.  
  6099. The IDs of all objects available within a program are strings that can
  6100. be expanded to the corresponding real objects with the
  6101. C<CPAN::Shell-E<gt>expand("Module",@things)> method. Expand returns a
  6102. list of CPAN::Module objects according to the C<@things> arguments
  6103. given. In scalar context it only returns the first element of the
  6104. list.
  6105.  
  6106. =item expandany(@things)
  6107.  
  6108. Like expand, but returns objects of the appropriate type, i.e.
  6109. CPAN::Bundle objects for bundles, CPAN::Module objects for modules and
  6110. CPAN::Distribution objects fro distributions.
  6111.  
  6112. =item Programming Examples
  6113.  
  6114. This enables the programmer to do operations that combine
  6115. functionalities that are available in the shell.
  6116.  
  6117.     # install everything that is outdated on my disk:
  6118.     perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
  6119.  
  6120.     # install my favorite programs if necessary:
  6121.     for $mod (qw(Net::FTP MD5 Data::Dumper)){
  6122.         my $obj = CPAN::Shell->expand('Module',$mod);
  6123.         $obj->install;
  6124.     }
  6125.  
  6126.     # list all modules on my disk that have no VERSION number
  6127.     for $mod (CPAN::Shell->expand("Module","/./")){
  6128.     next unless $mod->inst_file;
  6129.         # MakeMaker convention for undefined $VERSION:
  6130.     next unless $mod->inst_version eq "undef";
  6131.     print "No VERSION in ", $mod->id, "\n";
  6132.     }
  6133.  
  6134.     # find out which distribution on CPAN contains a module:
  6135.     print CPAN::Shell->expand("Module","Apache::Constants")->cpan_file
  6136.  
  6137. Or if you want to write a cronjob to watch The CPAN, you could list
  6138. all modules that need updating. First a quick and dirty way:
  6139.  
  6140.     perl -e 'use CPAN; CPAN::Shell->r;'
  6141.  
  6142. If you don't want to get any output in the case that all modules are
  6143. up to date, you can parse the output of above command for the regular
  6144. expression //modules are up to date// and decide to mail the output
  6145. only if it doesn't match. Ick?
  6146.  
  6147. If you prefer to do it more in a programmer style in one single
  6148. process, maybe something like this suits you better:
  6149.  
  6150.   # list all modules on my disk that have newer versions on CPAN
  6151.   for $mod (CPAN::Shell->expand("Module","/./")){
  6152.     next unless $mod->inst_file;
  6153.     next if $mod->uptodate;
  6154.     printf "Module %s is installed as %s, could be updated to %s from CPAN\n",
  6155.         $mod->id, $mod->inst_version, $mod->cpan_version;
  6156.   }
  6157.  
  6158. If that gives you too much output every day, you maybe only want to
  6159. watch for three modules. You can write
  6160.  
  6161.   for $mod (CPAN::Shell->expand("Module","/Apache|LWP|CGI/")){
  6162.  
  6163. as the first line instead. Or you can combine some of the above
  6164. tricks:
  6165.  
  6166.   # watch only for a new mod_perl module
  6167.   $mod = CPAN::Shell->expand("Module","mod_perl");
  6168.   exit if $mod->uptodate;
  6169.   # new mod_perl arrived, let me know all update recommendations
  6170.   CPAN::Shell->r;
  6171.  
  6172. =back
  6173.  
  6174. =head2 Methods in the other Classes
  6175.  
  6176. The programming interface for the classes CPAN::Module,
  6177. CPAN::Distribution, CPAN::Bundle, and CPAN::Author is still considered
  6178. beta and partially even alpha. In the following paragraphs only those
  6179. methods are documented that have proven useful over a longer time and
  6180. thus are unlikely to change.
  6181.  
  6182. =over
  6183.  
  6184. =item CPAN::Author::as_glimpse()
  6185.  
  6186. Returns a one-line description of the author
  6187.  
  6188. =item CPAN::Author::as_string()
  6189.  
  6190. Returns a multi-line description of the author
  6191.  
  6192. =item CPAN::Author::email()
  6193.  
  6194. Returns the author's email address
  6195.  
  6196. =item CPAN::Author::fullname()
  6197.  
  6198. Returns the author's name
  6199.  
  6200. =item CPAN::Author::name()
  6201.  
  6202. An alias for fullname
  6203.  
  6204. =item CPAN::Bundle::as_glimpse()
  6205.  
  6206. Returns a one-line description of the bundle
  6207.  
  6208. =item CPAN::Bundle::as_string()
  6209.  
  6210. Returns a multi-line description of the bundle
  6211.  
  6212. =item CPAN::Bundle::clean()
  6213.  
  6214. Recursively runs the C<clean> method on all items contained in the bundle.
  6215.  
  6216. =item CPAN::Bundle::contains()
  6217.  
  6218. Returns a list of objects' IDs contained in a bundle. The associated
  6219. objects may be bundles, modules or distributions.
  6220.  
  6221. =item CPAN::Bundle::force($method,@args)
  6222.  
  6223. Forces CPAN to perform a task that normally would have failed. Force
  6224. takes as arguments a method name to be called and any number of
  6225. additional arguments that should be passed to the called method. The
  6226. internals of the object get the needed changes so that CPAN.pm does
  6227. not refuse to take the action. The C<force> is passed recursively to
  6228. all contained objects.
  6229.  
  6230. =item CPAN::Bundle::get()
  6231.  
  6232. Recursively runs the C<get> method on all items contained in the bundle
  6233.  
  6234. =item CPAN::Bundle::inst_file()
  6235.  
  6236. Returns the highest installed version of the bundle in either @INC or
  6237. C<$CPAN::Config->{cpan_home}>. Note that this is different from
  6238. CPAN::Module::inst_file.
  6239.  
  6240. =item CPAN::Bundle::inst_version()
  6241.  
  6242. Like CPAN::Bundle::inst_file, but returns the $VERSION
  6243.  
  6244. =item CPAN::Bundle::uptodate()
  6245.  
  6246. Returns 1 if the bundle itself and all its members are uptodate.
  6247.  
  6248. =item CPAN::Bundle::install()
  6249.  
  6250. Recursively runs the C<install> method on all items contained in the bundle
  6251.  
  6252. =item CPAN::Bundle::make()
  6253.  
  6254. Recursively runs the C<make> method on all items contained in the bundle
  6255.  
  6256. =item CPAN::Bundle::readme()
  6257.  
  6258. Recursively runs the C<readme> method on all items contained in the bundle
  6259.  
  6260. =item CPAN::Bundle::test()
  6261.  
  6262. Recursively runs the C<test> method on all items contained in the bundle
  6263.  
  6264. =item CPAN::Distribution::as_glimpse()
  6265.  
  6266. Returns a one-line description of the distribution
  6267.  
  6268. =item CPAN::Distribution::as_string()
  6269.  
  6270. Returns a multi-line description of the distribution
  6271.  
  6272. =item CPAN::Distribution::clean()
  6273.  
  6274. Changes to the directory where the distribution has been unpacked and
  6275. runs C<make clean> there.
  6276.  
  6277. =item CPAN::Distribution::containsmods()
  6278.  
  6279. Returns a list of IDs of modules contained in a distribution file.
  6280. Only works for distributions listed in the 02packages.details.txt.gz
  6281. file. This typically means that only the most recent version of a
  6282. distribution is covered.
  6283.  
  6284. =item CPAN::Distribution::cvs_import()
  6285.  
  6286. Changes to the directory where the distribution has been unpacked and
  6287. runs something like
  6288.  
  6289.     cvs -d $cvs_root import -m $cvs_log $cvs_dir $userid v$version
  6290.  
  6291. there.
  6292.  
  6293. =item CPAN::Distribution::dir()
  6294.  
  6295. Returns the directory into which this distribution has been unpacked.
  6296.  
  6297. =item CPAN::Distribution::force($method,@args)
  6298.  
  6299. Forces CPAN to perform a task that normally would have failed. Force
  6300. takes as arguments a method name to be called and any number of
  6301. additional arguments that should be passed to the called method. The
  6302. internals of the object get the needed changes so that CPAN.pm does
  6303. not refuse to take the action.
  6304.  
  6305. =item CPAN::Distribution::get()
  6306.  
  6307. Downloads the distribution from CPAN and unpacks it. Does nothing if
  6308. the distribution has already been downloaded and unpacked within the
  6309. current session.
  6310.  
  6311. =item CPAN::Distribution::install()
  6312.  
  6313. Changes to the directory where the distribution has been unpacked and
  6314. runs the external command C<make install> there. If C<make> has not
  6315. yet been run, it will be run first. A C<make test> will be issued in
  6316. any case and if this fails, the install will be cancelled. The
  6317. cancellation can be avoided by letting C<force> run the C<install> for
  6318. you.
  6319.  
  6320. =item CPAN::Distribution::isa_perl()
  6321.  
  6322. Returns 1 if this distribution file seems to be a perl distribution.
  6323. Normally this is derived from the file name only, but the index from
  6324. CPAN can contain a hint to achieve a return value of true for other
  6325. filenames too.
  6326.  
  6327. =item CPAN::Distribution::look()
  6328.  
  6329. Changes to the directory where the distribution has been unpacked and
  6330. opens a subshell there. Exiting the subshell returns.
  6331.  
  6332. =item CPAN::Distribution::make()
  6333.  
  6334. First runs the C<get> method to make sure the distribution is
  6335. downloaded and unpacked. Changes to the directory where the
  6336. distribution has been unpacked and runs the external commands C<perl
  6337. Makefile.PL> and C<make> there.
  6338.  
  6339. =item CPAN::Distribution::prereq_pm()
  6340.  
  6341. Returns the hash reference that has been announced by a distribution
  6342. as the PREREQ_PM hash in the Makefile.PL. Note: works only after an
  6343. attempt has been made to C<make> the distribution. Returns undef
  6344. otherwise.
  6345.  
  6346. =item CPAN::Distribution::readme()
  6347.  
  6348. Downloads the README file associated with a distribution and runs it
  6349. through the pager specified in C<$CPAN::Config->{pager}>.
  6350.  
  6351. =item CPAN::Distribution::test()
  6352.  
  6353. Changes to the directory where the distribution has been unpacked and
  6354. runs C<make test> there.
  6355.  
  6356. =item CPAN::Distribution::uptodate()
  6357.  
  6358. Returns 1 if all the modules contained in the distribution are
  6359. uptodate. Relies on containsmods.
  6360.  
  6361. =item CPAN::Index::force_reload()
  6362.  
  6363. Forces a reload of all indices.
  6364.  
  6365. =item CPAN::Index::reload()
  6366.  
  6367. Reloads all indices if they have been read more than
  6368. C<$CPAN::Config->{index_expire}> days.
  6369.  
  6370. =item CPAN::InfoObj::dump()
  6371.  
  6372. CPAN::Author, CPAN::Bundle, CPAN::Module, and CPAN::Distribution
  6373. inherit this method. It prints the data structure associated with an
  6374. object. Useful for debugging. Note: the data structure is considered
  6375. internal and thus subject to change without notice.
  6376.  
  6377. =item CPAN::Module::as_glimpse()
  6378.  
  6379. Returns a one-line description of the module
  6380.  
  6381. =item CPAN::Module::as_string()
  6382.  
  6383. Returns a multi-line description of the module
  6384.  
  6385. =item CPAN::Module::clean()
  6386.  
  6387. Runs a clean on the distribution associated with this module.
  6388.  
  6389. =item CPAN::Module::cpan_file()
  6390.  
  6391. Returns the filename on CPAN that is associated with the module.
  6392.  
  6393. =item CPAN::Module::cpan_version()
  6394.  
  6395. Returns the latest version of this module available on CPAN.
  6396.  
  6397. =item CPAN::Module::cvs_import()
  6398.  
  6399. Runs a cvs_import on the distribution associated with this module.
  6400.  
  6401. =item CPAN::Module::description()
  6402.  
  6403. Returns a 44 chracter description of this module. Only available for
  6404. modules listed in The Module List (CPAN/modules/00modlist.long.html
  6405. or 00modlist.long.txt.gz)
  6406.  
  6407. =item CPAN::Module::force($method,@args)
  6408.  
  6409. Forces CPAN to perform a task that normally would have failed. Force
  6410. takes as arguments a method name to be called and any number of
  6411. additional arguments that should be passed to the called method. The
  6412. internals of the object get the needed changes so that CPAN.pm does
  6413. not refuse to take the action.
  6414.  
  6415. =item CPAN::Module::get()
  6416.  
  6417. Runs a get on the distribution associated with this module.
  6418.  
  6419. =item CPAN::Module::inst_file()
  6420.  
  6421. Returns the filename of the module found in @INC. The first file found
  6422. is reported just like perl itself stops searching @INC when it finds a
  6423. module.
  6424.  
  6425. =item CPAN::Module::inst_version()
  6426.  
  6427. Returns the version number of the module in readable format.
  6428.  
  6429. =item CPAN::Module::install()
  6430.  
  6431. Runs an C<install> on the distribution associated with this module.
  6432.  
  6433. =item CPAN::Module::look()
  6434.  
  6435. Changes to the directory where the distribution assoicated with this
  6436. module has been unpacked and opens a subshell there. Exiting the
  6437. subshell returns.
  6438.  
  6439. =item CPAN::Module::make()
  6440.  
  6441. Runs a C<make> on the distribution associated with this module.
  6442.  
  6443. =item CPAN::Module::manpage_headline()
  6444.  
  6445. If module is installed, peeks into the module's manpage, reads the
  6446. headline and returns it. Moreover, if the module has been downloaded
  6447. within this session, does the equivalent on the downloaded module even
  6448. if it is not installed.
  6449.  
  6450. =item CPAN::Module::readme()
  6451.  
  6452. Runs a C<readme> on the distribution associated with this module.
  6453.  
  6454. =item CPAN::Module::test()
  6455.  
  6456. Runs a C<test> on the distribution associated with this module.
  6457.  
  6458. =item CPAN::Module::uptodate()
  6459.  
  6460. Returns 1 if the module is installed and up-to-date.
  6461.  
  6462. =item CPAN::Module::userid()
  6463.  
  6464. Returns the author's ID of the module.
  6465.  
  6466. =back
  6467.  
  6468. =head2 Cache Manager
  6469.  
  6470. Currently the cache manager only keeps track of the build directory
  6471. ($CPAN::Config->{build_dir}). It is a simple FIFO mechanism that
  6472. deletes complete directories below C<build_dir> as soon as the size of
  6473. all directories there gets bigger than $CPAN::Config->{build_cache}
  6474. (in MB). The contents of this cache may be used for later
  6475. re-installations that you intend to do manually, but will never be
  6476. trusted by CPAN itself. This is due to the fact that the user might
  6477. use these directories for building modules on different architectures.
  6478.  
  6479. There is another directory ($CPAN::Config->{keep_source_where}) where
  6480. the original distribution files are kept. This directory is not
  6481. covered by the cache manager and must be controlled by the user. If
  6482. you choose to have the same directory as build_dir and as
  6483. keep_source_where directory, then your sources will be deleted with
  6484. the same fifo mechanism.
  6485.  
  6486. =head2 Bundles
  6487.  
  6488. A bundle is just a perl module in the namespace Bundle:: that does not
  6489. define any functions or methods. It usually only contains documentation.
  6490.  
  6491. It starts like a perl module with a package declaration and a $VERSION
  6492. variable. After that the pod section looks like any other pod with the
  6493. only difference being that I<one special pod section> exists starting with
  6494. (verbatim):
  6495.  
  6496.     =head1 CONTENTS
  6497.  
  6498. In this pod section each line obeys the format
  6499.  
  6500.         Module_Name [Version_String] [- optional text]
  6501.  
  6502. The only required part is the first field, the name of a module
  6503. (e.g. Foo::Bar, ie. I<not> the name of the distribution file). The rest
  6504. of the line is optional. The comment part is delimited by a dash just
  6505. as in the man page header.
  6506.  
  6507. The distribution of a bundle should follow the same convention as
  6508. other distributions.
  6509.  
  6510. Bundles are treated specially in the CPAN package. If you say 'install
  6511. Bundle::Tkkit' (assuming such a bundle exists), CPAN will install all
  6512. the modules in the CONTENTS section of the pod. You can install your
  6513. own Bundles locally by placing a conformant Bundle file somewhere into
  6514. your @INC path. The autobundle() command which is available in the
  6515. shell interface does that for you by including all currently installed
  6516. modules in a snapshot bundle file.
  6517.  
  6518. =head2 Prerequisites
  6519.  
  6520. If you have a local mirror of CPAN and can access all files with
  6521. "file:" URLs, then you only need a perl better than perl5.003 to run
  6522. this module. Otherwise Net::FTP is strongly recommended. LWP may be
  6523. required for non-UNIX systems or if your nearest CPAN site is
  6524. associated with an URL that is not C<ftp:>.
  6525.  
  6526. If you have neither Net::FTP nor LWP, there is a fallback mechanism
  6527. implemented for an external ftp command or for an external lynx
  6528. command.
  6529.  
  6530. =head2 Finding packages and VERSION
  6531.  
  6532. This module presumes that all packages on CPAN
  6533.  
  6534. =over 2
  6535.  
  6536. =item *
  6537.  
  6538. declare their $VERSION variable in an easy to parse manner. This
  6539. prerequisite can hardly be relaxed because it consumes far too much
  6540. memory to load all packages into the running program just to determine
  6541. the $VERSION variable. Currently all programs that are dealing with
  6542. version use something like this
  6543.  
  6544.     perl -MExtUtils::MakeMaker -le \
  6545.         'print MM->parse_version(shift)' filename
  6546.  
  6547. If you are author of a package and wonder if your $VERSION can be
  6548. parsed, please try the above method.
  6549.  
  6550. =item *
  6551.  
  6552. come as compressed or gzipped tarfiles or as zip files and contain a
  6553. Makefile.PL (well, we try to handle a bit more, but without much
  6554. enthusiasm).
  6555.  
  6556. =back
  6557.  
  6558. =head2 Debugging
  6559.  
  6560. The debugging of this module is a bit complex, because we have
  6561. interferences of the software producing the indices on CPAN, of the
  6562. mirroring process on CPAN, of packaging, of configuration, of
  6563. synchronicity, and of bugs within CPAN.pm.
  6564.  
  6565. For code debugging in interactive mode you can try "o debug" which
  6566. will list options for debugging the various parts of the code. You
  6567. should know that "o debug" has built-in completion support.
  6568.  
  6569. For data debugging there is the C<dump> command which takes the same
  6570. arguments as make/test/install and outputs the object's Data::Dumper
  6571. dump.
  6572.  
  6573. =head2 Floppy, Zip, Offline Mode
  6574.  
  6575. CPAN.pm works nicely without network too. If you maintain machines
  6576. that are not networked at all, you should consider working with file:
  6577. URLs. Of course, you have to collect your modules somewhere first. So
  6578. you might use CPAN.pm to put together all you need on a networked
  6579. machine. Then copy the $CPAN::Config->{keep_source_where} (but not
  6580. $CPAN::Config->{build_dir}) directory on a floppy. This floppy is kind
  6581. of a personal CPAN. CPAN.pm on the non-networked machines works nicely
  6582. with this floppy. See also below the paragraph about CD-ROM support.
  6583.  
  6584. =head1 CONFIGURATION
  6585.  
  6586. When the CPAN module is installed, a site wide configuration file is
  6587. created as CPAN/Config.pm. The default values defined there can be
  6588. overridden in another configuration file: CPAN/MyConfig.pm. You can
  6589. store this file in $HOME/.cpan/CPAN/MyConfig.pm if you want, because
  6590. $HOME/.cpan is added to the search path of the CPAN module before the
  6591. use() or require() statements.
  6592.  
  6593. Currently the following keys in the hash reference $CPAN::Config are
  6594. defined:
  6595.  
  6596.   build_cache        size of cache for directories to build modules
  6597.   build_dir          locally accessible directory to build modules
  6598.   index_expire       after this many days refetch index files
  6599.   cache_metadata     use serializer to cache metadata
  6600.   cpan_home          local directory reserved for this package
  6601.   dontload_hash      anonymous hash: modules in the keys will not be
  6602.                      loaded by the CPAN::has_inst() routine
  6603.   gzip             location of external program gzip
  6604.   inactivity_timeout breaks interactive Makefile.PLs after this
  6605.                      many seconds inactivity. Set to 0 to never break.
  6606.   inhibit_startup_message
  6607.                      if true, does not print the startup message
  6608.   keep_source_where  directory in which to keep the source (if we do)
  6609.   make               location of external make program
  6610.   make_arg         arguments that should always be passed to 'make'
  6611.   make_install_arg   same as make_arg for 'make install'
  6612.   makepl_arg         arguments passed to 'perl Makefile.PL'
  6613.   pager              location of external program more (or any pager)
  6614.   prerequisites_policy
  6615.                      what to do if you are missing module prerequisites
  6616.                      ('follow' automatically, 'ask' me, or 'ignore')
  6617.   proxy_user         username for accessing an authenticating proxy
  6618.   proxy_pass         password for accessing an authenticating proxy
  6619.   scan_cache         controls scanning of cache ('atstart' or 'never')
  6620.   tar                location of external program tar
  6621.   term_is_latin      if true internal UTF-8 is translated to ISO-8859-1
  6622.                      (and nonsense for characters outside latin range)
  6623.   unzip              location of external program unzip
  6624.   urllist         arrayref to nearby CPAN sites (or equivalent locations)
  6625.   wait_list          arrayref to a wait server to try (See CPAN::WAIT)
  6626.   ftp_proxy,      }  the three usual variables for configuring
  6627.     http_proxy,   }  proxy requests. Both as CPAN::Config variables
  6628.     no_proxy      }  and as environment variables configurable.
  6629.  
  6630. You can set and query each of these options interactively in the cpan
  6631. shell with the command set defined within the C<o conf> command:
  6632.  
  6633. =over 2
  6634.  
  6635. =item C<o conf E<lt>scalar optionE<gt>>
  6636.  
  6637. prints the current value of the I<scalar option>
  6638.  
  6639. =item C<o conf E<lt>scalar optionE<gt> E<lt>valueE<gt>>
  6640.  
  6641. Sets the value of the I<scalar option> to I<value>
  6642.  
  6643. =item C<o conf E<lt>list optionE<gt>>
  6644.  
  6645. prints the current value of the I<list option> in MakeMaker's
  6646. neatvalue format.
  6647.  
  6648. =item C<o conf E<lt>list optionE<gt> [shift|pop]>
  6649.  
  6650. shifts or pops the array in the I<list option> variable
  6651.  
  6652. =item C<o conf E<lt>list optionE<gt> [unshift|push|splice] E<lt>listE<gt>>
  6653.  
  6654. works like the corresponding perl commands.
  6655.  
  6656. =back
  6657.  
  6658. =head2 Note on urllist parameter's format
  6659.  
  6660. urllist parameters are URLs according to RFC 1738. We do a little
  6661. guessing if your URL is not compliant, but if you have problems with
  6662. file URLs, please try the correct format. Either:
  6663.  
  6664.     file://localhost/whatever/ftp/pub/CPAN/
  6665.  
  6666. or
  6667.  
  6668.     file:///home/ftp/pub/CPAN/
  6669.  
  6670. =head2 urllist parameter has CD-ROM support
  6671.  
  6672. The C<urllist> parameter of the configuration table contains a list of
  6673. URLs that are to be used for downloading. If the list contains any
  6674. C<file> URLs, CPAN always tries to get files from there first. This
  6675. feature is disabled for index files. So the recommendation for the
  6676. owner of a CD-ROM with CPAN contents is: include your local, possibly
  6677. outdated CD-ROM as a C<file> URL at the end of urllist, e.g.
  6678.  
  6679.   o conf urllist push file://localhost/CDROM/CPAN
  6680.  
  6681. CPAN.pm will then fetch the index files from one of the CPAN sites
  6682. that come at the beginning of urllist. It will later check for each
  6683. module if there is a local copy of the most recent version.
  6684.  
  6685. Another peculiarity of urllist is that the site that we could
  6686. successfully fetch the last file from automatically gets a preference
  6687. token and is tried as the first site for the next request. So if you
  6688. add a new site at runtime it may happen that the previously preferred
  6689. site will be tried another time. This means that if you want to disallow
  6690. a site for the next transfer, it must be explicitly removed from
  6691. urllist.
  6692.  
  6693. =head1 SECURITY
  6694.  
  6695. There's no strong security layer in CPAN.pm. CPAN.pm helps you to
  6696. install foreign, unmasked, unsigned code on your machine. We compare
  6697. to a checksum that comes from the net just as the distribution file
  6698. itself. If somebody has managed to tamper with the distribution file,
  6699. they may have as well tampered with the CHECKSUMS file. Future
  6700. development will go towards strong authentication.
  6701.  
  6702. =head1 EXPORT
  6703.  
  6704. Most functions in package CPAN are exported per default. The reason
  6705. for this is that the primary use is intended for the cpan shell or for
  6706. oneliners.
  6707.  
  6708. =head1 POPULATE AN INSTALLATION WITH LOTS OF MODULES
  6709.  
  6710. Populating a freshly installed perl with my favorite modules is pretty
  6711. easy if you maintain a private bundle definition file. To get a useful
  6712. blueprint of a bundle definition file, the command autobundle can be used
  6713. on the CPAN shell command line. This command writes a bundle definition
  6714. file for all modules that are installed for the currently running perl
  6715. interpreter. It's recommended to run this command only once and from then
  6716. on maintain the file manually under a private name, say
  6717. Bundle/my_bundle.pm. With a clever bundle file you can then simply say
  6718.  
  6719.     cpan> install Bundle::my_bundle
  6720.  
  6721. then answer a few questions and then go out for a coffee.
  6722.  
  6723. Maintaining a bundle definition file means keeping track of two
  6724. things: dependencies and interactivity. CPAN.pm sometimes fails on
  6725. calculating dependencies because not all modules define all MakeMaker
  6726. attributes correctly, so a bundle definition file should specify
  6727. prerequisites as early as possible. On the other hand, it's a bit
  6728. annoying that many distributions need some interactive configuring. So
  6729. what I try to accomplish in my private bundle file is to have the
  6730. packages that need to be configured early in the file and the gentle
  6731. ones later, so I can go out after a few minutes and leave CPAN.pm
  6732. untended.
  6733.  
  6734. =head1 WORKING WITH CPAN.pm BEHIND FIREWALLS
  6735.  
  6736. Thanks to Graham Barr for contributing the following paragraphs about
  6737. the interaction between perl, and various firewall configurations. For
  6738. further informations on firewalls, it is recommended to consult the
  6739. documentation that comes with the ncftp program. If you are unable to
  6740. go through the firewall with a simple Perl setup, it is very likely
  6741. that you can configure ncftp so that it works for your firewall.
  6742.  
  6743. =head2 Three basic types of firewalls
  6744.  
  6745. Firewalls can be categorized into three basic types.
  6746.  
  6747. =over
  6748.  
  6749. =item http firewall
  6750.  
  6751. This is where the firewall machine runs a web server and to access the
  6752. outside world you must do it via the web server. If you set environment
  6753. variables like http_proxy or ftp_proxy to a values beginning with http://
  6754. or in your web browser you have to set proxy information then you know
  6755. you are running a http firewall.
  6756.  
  6757. To access servers outside these types of firewalls with perl (even for
  6758. ftp) you will need to use LWP.
  6759.  
  6760. =item ftp firewall
  6761.  
  6762. This where the firewall machine runs a ftp server. This kind of
  6763. firewall will only let you access ftp servers outside the firewall.
  6764. This is usually done by connecting to the firewall with ftp, then
  6765. entering a username like "user@outside.host.com"
  6766.  
  6767. To access servers outside these type of firewalls with perl you
  6768. will need to use Net::FTP.
  6769.  
  6770. =item One way visibility
  6771.  
  6772. I say one way visibility as these firewalls try to make themselve look
  6773. invisible to the users inside the firewall. An FTP data connection is
  6774. normally created by sending the remote server your IP address and then
  6775. listening for the connection. But the remote server will not be able to
  6776. connect to you because of the firewall. So for these types of firewall
  6777. FTP connections need to be done in a passive mode.
  6778.  
  6779. There are two that I can think off.
  6780.  
  6781. =over
  6782.  
  6783. =item SOCKS
  6784.  
  6785. If you are using a SOCKS firewall you will need to compile perl and link
  6786. it with the SOCKS library, this is what is normally called a 'socksified'
  6787. perl. With this executable you will be able to connect to servers outside
  6788. the firewall as if it is not there.
  6789.  
  6790. =item IP Masquerade
  6791.  
  6792. This is the firewall implemented in the Linux kernel, it allows you to
  6793. hide a complete network behind one IP address. With this firewall no
  6794. special compiling is needed as you can access hosts directly.
  6795.  
  6796. =back
  6797.  
  6798. =back
  6799.  
  6800. =head2 Configuring lynx or ncftp for going through a firewall
  6801.  
  6802. If you can go through your firewall with e.g. lynx, presumably with a
  6803. command such as
  6804.  
  6805.     /usr/local/bin/lynx -pscott:tiger
  6806.  
  6807. then you would configure CPAN.pm with the command
  6808.  
  6809.     o conf lynx "/usr/local/bin/lynx -pscott:tiger"
  6810.  
  6811. That's all. Similarly for ncftp or ftp, you would configure something
  6812. like
  6813.  
  6814.     o conf ncftp "/usr/bin/ncftp -f /home/scott/ncftplogin.cfg"
  6815.  
  6816. Your milage may vary...
  6817.  
  6818. =head1 FAQ
  6819.  
  6820. =over
  6821.  
  6822. =item 1)
  6823.  
  6824. I installed a new version of module X but CPAN keeps saying,
  6825. I have the old version installed
  6826.  
  6827. Most probably you B<do> have the old version installed. This can
  6828. happen if a module installs itself into a different directory in the
  6829. @INC path than it was previously installed. This is not really a
  6830. CPAN.pm problem, you would have the same problem when installing the
  6831. module manually. The easiest way to prevent this behaviour is to add
  6832. the argument C<UNINST=1> to the C<make install> call, and that is why
  6833. many people add this argument permanently by configuring
  6834.  
  6835.   o conf make_install_arg UNINST=1
  6836.  
  6837. =item 2)
  6838.  
  6839. So why is UNINST=1 not the default?
  6840.  
  6841. Because there are people who have their precise expectations about who
  6842. may install where in the @INC path and who uses which @INC array. In
  6843. fine tuned environments C<UNINST=1> can cause damage.
  6844.  
  6845. =item 3)
  6846.  
  6847. I want to clean up my mess, and install a new perl along with
  6848. all modules I have. How do I go about it?
  6849.  
  6850. Run the autobundle command for your old perl and optionally rename the
  6851. resulting bundle file (e.g. Bundle/mybundle.pm), install the new perl
  6852. with the Configure option prefix, e.g.
  6853.  
  6854.     ./Configure -Dprefix=/usr/local/perl-5.6.78.9
  6855.  
  6856. Install the bundle file you produced in the first step with something like
  6857.  
  6858.     cpan> install Bundle::mybundle
  6859.  
  6860. and you're done.
  6861.  
  6862. =item 4)
  6863.  
  6864. When I install bundles or multiple modules with one command
  6865. there is too much output to keep track of.
  6866.  
  6867. You may want to configure something like
  6868.  
  6869.   o conf make_arg "| tee -ai /root/.cpan/logs/make.out"
  6870.   o conf make_install_arg "| tee -ai /root/.cpan/logs/make_install.out"
  6871.  
  6872. so that STDOUT is captured in a file for later inspection.
  6873.  
  6874.  
  6875. =item 5)
  6876.  
  6877. I am not root, how can I install a module in a personal directory?
  6878.  
  6879. You will most probably like something like this:
  6880.  
  6881.   o conf makepl_arg "LIB=~/myperl/lib \
  6882.                     INSTALLMAN1DIR=~/myperl/man/man1 \
  6883.                     INSTALLMAN3DIR=~/myperl/man/man3"
  6884.   install Sybase::Sybperl
  6885.  
  6886. You can make this setting permanent like all C<o conf> settings with
  6887. C<o conf commit>.
  6888.  
  6889. You will have to add ~/myperl/man to the MANPATH environment variable
  6890. and also tell your perl programs to look into ~/myperl/lib, e.g. by
  6891. including
  6892.  
  6893.   use lib "$ENV{HOME}/myperl/lib";
  6894.  
  6895. or setting the PERL5LIB environment variable.
  6896.  
  6897. Another thing you should bear in mind is that the UNINST parameter
  6898. should never be set if you are not root.
  6899.  
  6900. =item 6)
  6901.  
  6902. How to get a package, unwrap it, and make a change before building it?
  6903.  
  6904.   look Sybase::Sybperl
  6905.  
  6906. =item 7)
  6907.  
  6908. I installed a Bundle and had a couple of fails. When I
  6909. retried, everything resolved nicely. Can this be fixed to work
  6910. on first try?
  6911.  
  6912. The reason for this is that CPAN does not know the dependencies of all
  6913. modules when it starts out. To decide about the additional items to
  6914. install, it just uses data found in the generated Makefile. An
  6915. undetected missing piece breaks the process. But it may well be that
  6916. your Bundle installs some prerequisite later than some depending item
  6917. and thus your second try is able to resolve everything. Please note,
  6918. CPAN.pm does not know the dependency tree in advance and cannot sort
  6919. the queue of things to install in a topologically correct order. It
  6920. resolves perfectly well IFF all modules declare the prerequisites
  6921. correctly with the PREREQ_PM attribute to MakeMaker. For bundles which
  6922. fail and you need to install often, it is recommended sort the Bundle
  6923. definition file manually. It is planned to improve the metadata
  6924. situation for dependencies on CPAN in general, but this will still
  6925. take some time.
  6926.  
  6927. =item 8)
  6928.  
  6929. In our intranet we have many modules for internal use. How
  6930. can I integrate these modules with CPAN.pm but without uploading
  6931. the modules to CPAN?
  6932.  
  6933. Have a look at the CPAN::Site module.
  6934.  
  6935. =item 9)
  6936.  
  6937. When I run CPAN's shell, I get error msg about line 1 to 4,
  6938. setting meta input/output via the /etc/inputrc file.
  6939.  
  6940. Some versions of readline are picky about capitalization in the
  6941. /etc/inputrc file and specifically RedHat 6.2 comes with a
  6942. /etc/inputrc that contains the word C<on> in lowercase. Change the
  6943. occurrences of C<on> to C<On> and the bug should disappear.
  6944.  
  6945. =item 10)
  6946.  
  6947. Some authors have strange characters in their names.
  6948.  
  6949. Internally CPAN.pm uses the UTF-8 charset. If your terminal is
  6950. expecting ISO-8859-1 charset, a converter can be activated by setting
  6951. term_is_latin to a true value in your config file. One way of doing so
  6952. would be
  6953.  
  6954.     cpan> ! $CPAN::Config->{term_is_latin}=1
  6955.  
  6956. Extended support for converters will be made available as soon as perl
  6957. becomes stable with regard to charset issues.
  6958.  
  6959. =back
  6960.  
  6961. =head1 BUGS
  6962.  
  6963. We should give coverage for B<all> of the CPAN and not just the PAUSE
  6964. part, right? In this discussion CPAN and PAUSE have become equal --
  6965. but they are not. PAUSE is authors/, modules/ and scripts/. CPAN is
  6966. PAUSE plus the clpa/, doc/, misc/, ports/, and src/.
  6967.  
  6968. Future development should be directed towards a better integration of
  6969. the other parts.
  6970.  
  6971. If a Makefile.PL requires special customization of libraries, prompts
  6972. the user for special input, etc. then you may find CPAN is not able to
  6973. build the distribution. In that case, you should attempt the
  6974. traditional method of building a Perl module package from a shell.
  6975.  
  6976. =head1 AUTHOR
  6977.  
  6978. Andreas Koenig E<lt>andreas.koenig@anima.deE<gt>
  6979.  
  6980. =head1 TRANSLATIONS
  6981.  
  6982. Kawai,Takanori provides a Japanese translation of this manpage at
  6983. http://member.nifty.ne.jp/hippo2000/perltips/CPAN.htm
  6984.  
  6985. =head1 SEE ALSO
  6986.  
  6987. perl(1), CPAN::Nox(3)
  6988.  
  6989. =cut
  6990.  
  6991.